2
votes

Working with Swift and Core-Data, I have the following function, which is useful to insert a new record in an entity:

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)
}

Though it works, there is one problem: the type "EventList" is hard-coded in the function and that is not good if I want the function for general use. Ideally I would like to pass a parameter: type to the function and then use it instead of "EventList".

Is that possible and how? What I have found on the net seems not clear and at the limit of my present Swift knowledge.

=== Update after further experiment ===

Following some advice I got from feed backs and after taking a look at how generics work, I remade my function as follows:

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)
}

I didn't get any error on a particular line of code, but got the following big error-message in the issue navigator:

CompileSwift normal 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/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/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&, swift::CanTypeWrapper, swift::CanTypeWrapper, llvm::ArrayRef, swift::irgen::Explosion&) + 630 5 swift 0x00000001001b6d0f swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::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&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 2121 9 swift 0x0000000100197693 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, 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 Stack dump: 0. Program arguments: /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/Build/Intermediates/MyAp.build/Debug-iphoneos/MyAp.build/Objects-normal/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

  1. While emitting IR SIL function @_TToFC2Me11CoreDataBox12insertObjectfS0_U__FTSS4dicoCSo12NSDictionary_T_ for 'insertObject' at /Users/abcxyz/Documents/iOS/LibBin/Swift_CoreDataBox/CoreDataBox.swift:98:5
1
That's probably a good advice, how do I do that? Is it easy (simple)?Michel
Don't have a compiler right now but it should be something along the lines func insertObject<T>(entityName: String, dictionary: NSDictionary and when you force cast it to an EventList cast it to T instead.HAS
Seems unnecessary to me. Will T accept the setValue method without further casting?Mundi
Thanks for the tips. I am looking at generics and how to use them. It is very interesting and seems similar to what I used to know as function templates. I don't know if the implementation is similar though.Michel

1 Answers

1
votes

You are just using KVC to set the attributes, so you do not need a specific NSManagedObject subclass.

Substituting the cast with one to NSManagedObject should work. Making it generic (in the Swift sense) would be overkill - after all, you need the NSManagedObject. So you are "generalizing" rather than using "generics".