michaelarmstrong / SuperRecord

A small set of utilities to make working with CoreData and Swift a bit easier.
MIT License
366 stars 27 forks source link

Remove @objc(className) #21

Closed PGLongo closed 9 years ago

PGLongo commented 9 years ago

In the current version of Swift NSStringFromClass(ClassName) returns // ProjectName.Pokemon.

So we can easily add a new function in NSManagedObject that returns only the ClassName without forcing the user to add @objc(ClassName) to each NSManagedObject.

What do you think @michaelarmstrong?

michaelarmstrong commented 9 years ago

I can't remember exactly why I added that in the first place, I think it had something to do with either Objective-C users consuming this Swift Framework or modules in Xcode not working as intended. Can you remember?

PGLongo commented 9 years ago

In previous version it was something like _StringIDon'tRememberClassName.

In current version is SuperRecord.Pokemon so we can add a function or a variable to compute the class name taking the second part of the string. Developer using and older version of Swift (poor poor guys) should use @objc(className) and new developer can avoid it.

I will try it today and make a PR.

PGLongo commented 9 years ago

I don't know the reason but it seems not working. I have removed @objc(Pokemon) and get the correct name but I received this error:

Unable to load class named 'Pokemon' for entity 'Pokemon'

The instruction @objc(Pokemon) is necessary for some other reason other than

NSStringFromClass(self)
michaelarmstrong commented 9 years ago

Yea, I literally can't remember why, but there was some important reason :) i'll try remember at some point.

michaelarmstrong commented 9 years ago

out of interest, did you assign the result of NSStringFromClass(self) to a String or NSString ?

PGLongo commented 9 years ago
var entityName : NSString = NSStringFromClass(self)

The problem is not the result but the retrieval of the NSManagedObject made by CoreData.

michaelarmstrong commented 9 years ago

yes, i've seen this before when using Objective-C CoreData in a Swift project and it was caused by functions returning NSString being casted to String.

I resolved it by doing

var entityName : NSString = NSStringFromClass(self) as NSString

but perhaps my magic-tricks don't work here :) i don't have time to dig into it properly now :(

PGLongo commented 9 years ago

Same error forcing cast as NSString

ikesyo commented 9 years ago

Entity names in xcdatamodel must be named like ModuleName.ClassName if @objc(ClassName) isnt' used. So for the test target, I think you should rename Pokemon to SuperRecordTests.Pokemon in model editor.

michaelarmstrong commented 9 years ago

indeed. However that wasn't working before. ikesyo, please make a PR and test :)

PGLongo commented 9 years ago

My idea was to avoid forcing the developer to add @objc(ClassName) or doing other strange things. For this reason I think that naming the Entity ModuleName.ClassName is not a solution!!

michaelarmstrong commented 9 years ago

agreed, its swapping 1 task for another. lets park this for now and see what changes in the final release of swift 1.2

ikesyo commented 9 years ago

I'd like to somehow demonstrate the entity naming case above. But I'm not with Mac now, it'll be tomorrow or the day after tomorrow :wink:

ikesyo commented 9 years ago

Entity names in xcdatamodel must be named like ModuleName.ClassName if @objc(ClassName) isnt' used.

Sorry, this wasn't correct. It's not the entity name, but the class name in model editor.

PGLongo commented 9 years ago

Don't worry. Thanks anyway