我正在尝试编写一组扩展,使得在swift中处理JSON对象变得更简单。以下是我设置的扩展代码示例:
import Foundation
extension NSData {
var JSONObject: AnyObject? {
return parseJSONObject(self)
}
}
extension String {
var JSONObject: AnyObject? {
return self.dataUsingEncoding(NSUTF8StringEncoding).JSONObject
}
}
func parseJSONObject(object: NSData) -> AnyObject? {
var error: NSError?
var result : AnyObject? = NSJSONSerialization.JSONObjectWithData(object as NSData, options: NSJSONReadingOptions.MutableContainers, error: &error)
if error {
println("Error parsing JSON object from: \(object)");
}
return result
}
当我将它用于String对象时,此代码有效,但它不适用于此文件外部的NSData对象。奇怪的是,String扩展调用NSData扩展代码并在文件中正常工作。有谁知道问题可能是什么?
import XCTest
let testJSONString = "{\"foo\":\"bar\"}"
class JSONTests: XCTestCase {
let testJSONData = testJSONString.dataUsingEncoding(NSUTF8StringEncoding)
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
// This test compiles with no problem, as I am using the extension on a Swift String.
func testParseString() {
var fooValue: String? = testJSONString.JSONObject!["foo"] as? String
XCTAssertTrue(fooValue == "bar", "Value for key 'foo' should be 'bar' for parsed JSON string")
}
// This method causes a compiler error I assume, because I am using the NSData extension
func testParseData() {
var fooValue: String? = testJSONData.JSONObject!["foo"] as? String
XCTAssertTrue(fooValue == "bar", "Value for key 'foo' should be 'bar' for parsed JSON string")
}
}
以下是我看到的错误:
CompileSwift normal i386 com.apple.xcode.tools.swift.compiler
cd /Users/phogan/Software/BanDedo/SwiftKit
export PATH="/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode6-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -target i386-apple-ios8.0 -module-name SwiftKitTests -O0 -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -g -module-cache-path /Users/phogan/Library/Developer/Xcode/DerivedData/ModuleCache -I /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Products/Debug-iphonesimulator -F /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Products/Debug-iphonesimulator -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -c -j8 /Users/phogan/Software/BanDedo/SwiftKit/SwiftKit/Application/PlanckTags.swift /Users/phogan/Software/BanDedo/SwiftKit/Belt/JSON.swift /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Belt/JSONTests.swift /Users/phogan/Software/BanDedo/SwiftKit/Planck/Planck.swift /Users/phogan/Software/BanDedo/SwiftKit/Belt/RingBuffer.swift /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Planck/PlanckTests.swift /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Belt/RingBufferTests.swift -output-file-map /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/SwiftKitTests-OutputFileMap.json -serialize-diagnostics -emit-dependencies -emit-module -emit-module-path /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/SwiftKitTests.swiftmodule -Xcc -iquote -Xcc /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-generated-files.hmap -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-own-target-headers.hmap -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-all-target-headers.hmap -Xcc -iquote -Xcc /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-project-headers.hmap -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/DerivedSources/i386 -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DDEBUG=1 -emit-objc-header -emit-objc-header-path /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/SwiftKitTests-Swift.h
0 swift 0x000000010c4c0608 llvm::sys::PrintStackTrace(__sFILE*) + 40
1 swift 0x000000010c4c0af4 SignalHandler(int) + 452
2 libsystem_platform.dylib 0x00007fff914b25aa _sigtramp + 26
3 libsystem_platform.dylib 0x0000000000000004 _sigtramp + 1857346164
4 swift 0x000000010b8c47b2 swift::irgen::emitVirtualMethodValue(swift::irgen::IRGenFunction&, llvm::Value*, swift::SILType, swift::SILDeclRef, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ResilienceExpansion) + 434
5 swift 0x000000010b9300d3 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 42611
6 swift 0x000000010b925266 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
7 swift 0x000000010b8a66f8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
8 swift 0x000000010b9126e3 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1859
9 swift 0x000000010b913033 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
10 swift 0x000000010b88565a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 4842
11 swift 0x000000010b88435d main + 1533
12 libdyld.dylib 0x00007fff8d5c55fd start + 1
13 libdyld.dylib 0x0000000000000042 start + 1923328582
Stack dump:
0. Program arguments: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/phogan/Software/BanDedo/SwiftKit/SwiftKit/Application/PlanckTags.swift /Users/phogan/Software/BanDedo/SwiftKit/Belt/JSON.swift -primary-file /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Belt/JSONTests.swift /Users/phogan/Software/BanDedo/SwiftKit/Planck/Planck.swift /Users/phogan/Software/BanDedo/SwiftKit/Belt/RingBuffer.swift /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Planck/PlanckTests.swift /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Belt/RingBufferTests.swift -enable-objc-attr-requires-objc-module -target i386-apple-ios8.0 -module-name SwiftKitTests -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -I /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Products/Debug-iphonesimulator -F /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Products/Debug-iphonesimulator -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -g -module-cache-path /Users/phogan/Library/Developer/Xcode/DerivedData/ModuleCache -Xcc -iquote -Xcc /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-generated-files.hmap -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-own-target-headers.hmap -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-all-target-headers.hmap -Xcc -iquote -Xcc /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/SwiftKitTests-project-headers.hmap -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/DerivedSources/i386 -Xcc -I/Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DDEBUG=1 -emit-module-doc-path /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/JSONTests~partial.swiftdoc -O0 -emit-module-path /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/JSONTests~partial.swiftmodule -serialize-diagnostics-path /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/JSONTests.dia -emit-dependencies-path /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/JSONTests.d -o /Users/phogan/Library/Developer/Xcode/DerivedData/SwiftKit-btmjklrksylmiohgevkkbihmktno/Build/Intermediates/SwiftKit.build/Debug-iphonesimulator/SwiftKitTests.build/Objects-normal/i386/JSONTests.o
1. While emitting IR SIL function @_TFC13SwiftKitTests9JSONTests13testParseDatafS0_FT_T_ for 'testParseData' at /Users/phogan/Software/BanDedo/SwiftKit/SwiftKitTests/Belt/JSONTests.swift:29:5
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254
答案 0 :(得分:0)
我遇到了类似的问题。
我有一个扩展,它接受一个NSString并返回NSData和另一个接收NSData并返回NSData的扩展。我可以将NSString扩展放在Xcode中的任何位置,它可以放在外部文件中,旁边是外部文件中的Class,ViewController,Main,App Delegate。对于我的NSData扩展,情况也是如此。它只会在放置在AppDelegate中时运行。
我计划将我创建的所有扩展程序放在一个单独的文件中,或者放在与其相关类相同的文件中。但现在作为一种解决方法,NSData ext必须存在于App Delegate文件中。我稍后会发布一些代码。现在这不是一个大问题,只是一个奇怪的事情。
// just runs in App Delegate
extension NSData {
var sha256:NSData { return myCrypto().dataToSHA256(self) }
}
// runs just in external swift file just fine.
extension String {
var hexToBytes:NSData { return myCrypto().hexToBytes(self) }
}
您可以使用伪代码,如果扩展名不在根级别的App Delegate中,NSData将始终出错。