使用Swift和Core-Data,我有以下功能,这对于在实体中插入新记录很有用:
func insertObject (entityName: String, dico: NSDictionary) {
var newItem = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext:managedObjectContext!) as! EventList
for (key, value) in dico {
newItem.setValue(value, forKey: key as! String)
}
var error : NSError? = nil
managedObjectContext!.save(&error)
}
虽然它有效,但有一个问题:类型" EventList"在函数中是硬编码的,如果我想要一般用途的功能,这是不好的。 理想情况下,我想传递一个参数:type到函数,然后使用它代替" EventList"。
这可能吗?怎么样?我在网上发现的内容似乎并不清楚,并且在我现在的Swift知识的极限下。
===进一步实验后更新===
根据一些建议,我从反馈中得到了一些建议,在看了泛型的工作原理后,我重新编写了我的功能如下:
func insertObject <T:NSManagedObject> (entityName:String,dico:NSDictionary) {
var newItem = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext:managedObjectContext!) as! T
for (key, value) in dico {
newItem.setValue(value, forKey: key as! String)
}
var error : NSError? = nil
managedObjectContext!.save(&error)
}
我没有在特定代码行上收到任何错误,但在问题导航器中收到以下重大错误消息:
CompileSwift正常armv7 /Users/abcxyz/Documents/iOS/LibBin/Swift_CoreDataBox/CoreDataBox.swift cd / Users / abcxyz / Documents / iOS / MyAp /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/abcxyz/Documents/iOS/LibBin/Swift_CoreDataBox/CoreDataBox.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/RootViewController.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/ModelController.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/DataViewController.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/MyAp.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/AppDelegate.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/DataInput_ViewController.swift -target armv7-apple-ios7.1 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Products / Debug-iphoneos -F / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Products / Debug-iphoneos -g -module-cache-path / Users / abcxyz / Library / Developer / Xcode / DerivedData / ModuleCache -serialize-debugging-options -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / swift-overrides.hmap -Xcc -iquote -Xcc /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/MyAp-generated-files.hmap -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / MyAp-own-target-headers.hmap -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / MyAp-all-target-headers.hmap -Xcc -iquote -Xcc /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/MyAp-project-headers.hmap -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Products / Debug-iphoneos / include -Xcc -I / Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / include -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / DerivedSources / armv7 -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / DerivedSources -Xcc -DDEBUG = 1 -Xcc -working-directory / Users / abcxyz / Documents / iOS / MyAp -emit-module-doc-path / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates /MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox~partial.swiftdoc -Onone -module-name Me -emit-module-path / Users / abcxyz / Library / Developer / Xcode / DerivedData / MYAP-ehmiuzpyuhwbkpfvduswfbhkdjut /建造/中间体/ MyAp.build /调试-的iPhoneOS / MyAp.build /对象 - 正常/ ARMv7的/ CoreDataBox〜partial.swiftmodule -serialize-diagnostics-path /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.dia -emit-dependencies-path /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.d -emit-reference-dependencies-path /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.swiftdeps -o /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.o
0 swift 0x00000001007e92b8 llvm :: sys :: PrintStackTrace(__ sFILE *)+ 40 1 swift
0x00000001007e9794 SignalHandler(int)+ 452 2 libsystem_platform.dylib 0x00007fff96272f1a _sigtramp + 26 3 libsystem_platform.dylib 0x7994a32ea448635d _sigtramp + 237057117 4 swift 0x000000010016c076 SWIFT :: irgen :: emitPolymorphicArguments(SWIFT :: irgen :: IRGenFunction和放大器;, 迅速:: CanTypeWrapper, 迅速:: CanTypeWrapper, llvm :: ArrayRef,swift :: irgen :: Explosion&amp;)+ 630 5 swift 0x00000001001b6d0f swift :: SILVisitor&lt;(匿名命名空间):: IRGenSILFunction, void&gt; :: visit(swift :: ValueBase *)+ 40287 6 swift
0x00000001001ac39d swift :: irgen :: IRGenModule :: emitSILFunction(swift :: SILFunction *)+ 9901 7 swift 0x0000000100116f4f swift :: irgen :: IRGenModule :: emitGlobalTopLevel()+ 159 8 swift
0x0000000100196c59 performIRGeneration(swift :: IRGenOptions&amp ;,, swift :: Module *,swift :: SILModule *,llvm :: StringRef, llvm :: LLVMContext&amp;,swift :: SourceFile *,unsigned int)+ 2121 9 swift 0x0000000100197693 swift :: performIRGeneration(swift :: IRGenOptions&amp;, swift :: SourceFile&amp;,swift :: SILModule *,llvm :: StringRef, llvm :: LLVMContext&amp;,unsigned int)+ 51 10 swift
0x00000001000d3087 frontend_main(llvm :: ArrayRef,char const *,void *)+ 6647 11 swift 0x00000001000d14e6 main + 1814 12 libdyld.dylib 0x00007fff947a35c9 start + 1 13 libdyld.dylib 0x0000000000000041 start + 1803930233 堆栈转储: 0.程序参数:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/abcxyz/Documents/iOS/LibBin/Swift_CoreDataBox/CoreDataBox.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/RootViewController.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/ModelController.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/DataViewController.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/MyAp.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/AppDelegate.swift /Users/abcxyz/Documents/iOS/MyAp/MyAp/DataInput_ViewController.swift -target armv7-apple-ios7.1 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Products / Debug-iphoneos -F / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Products / Debug-iphoneos -g -module-cache-path / Users / abcxyz / Library / Developer / Xcode / DerivedData / ModuleCache -serialize-debugging-options -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / swift-overrides.hmap -Xcc -iquote -Xcc /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/MyAp-generated-files.hmap -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / MyAp-own-target-headers.hmap -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / MyAp-all-target-headers.hmap -Xcc -iquote -Xcc /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/MyAp-project-headers.hmap -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Products / Debug-iphoneos / include -Xcc -I / Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / include -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / DerivedSources / armv7 -Xcc -I / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates / MyAp.build / Debug-iphoneos / MyAp.build / DerivedSources -Xcc -DDEBUG = 1 -Xcc -working-directory / Users / abcxyz / Documents / iOS / MyAp -emit-module-doc-path / Users / abcxyz / Library / Developer / Xcode / DerivedData / MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut / Build / Intermediates /MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox~partial.swiftdoc -Onone -module-name Me -emit-module-path / Users / abcxyz / Library / Developer / Xcode / DerivedData / MYAP-ehmiuzpyuhwbkpfvduswfbhkdjut /建造/中间体/ MyAp.build /调试-的iPhoneOS / MyAp.build /对象 - 正常/ ARMv7的/ CoreDataBox〜partial.swiftmodule -serialize-diagnostics-path /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.dia -emit-dependencies-path /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.d -emit-reference-dependencies-path /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.swiftdeps -o /Users/abcxyz/Library/Developer/Xcode/DerivedData/MyAp-ehmiuzpyuhwbkpfvduswfbhkdjut/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/armv7/CoreDataBox.o
- 发射IR SIL功能时@TToFC2Me11CoreDataBox12insertObjectfS0_U__FTSS4dicoCSo12NSDictionary_T_ for&#39; insertObject&#39;在 /Users/abcxyz/Documents/iOS/LibBin/Swift_CoreDataBox/CoreDataBox.swift:98:5
醇>
答案 0 :(得分:1)
您只是使用KVC来设置属性,因此您不需要特定的NSManagedObject
子类。
用一个转换为NSManagedObject
取代应该有效。使它成为通用的(在Swift意义上)将是过度的 - 毕竟,你需要NSManagedObject
。所以你是“推广”而不是使用“泛型”。