3

I have some code in an app that creates a CoreData managed object. In this code, I use the following line to set a property:

theAuthor.authorID = 1;

The property is declared like this in the managed object header:

@property (nonatomic) uint32_t authorID;

In iOS 5 it works fine, but when I debug in iOS 4.3, I get this error:

Property 'authorID' is a scalar type on class 'Author'. Cannot generate a setter method for it.

Why am I getting this error in 4.3, but not in 5? Should I be avoiding scalar properties? I came to Obj-C from C, so I prefer to work with scalars when I can, as it feels more optimised.

Would I be better implementing the getters and setters or changing my code to use NSInteger or NSNumber instead?

colincameron
  • 2,696
  • 4
  • 23
  • 46
  • See if [this SO post](http://stackoverflow.com/questions/1152025/objective-c-error-property-active-is-a-scalar-type-on-class-routine-cannot) helps you. – EmptyStack Oct 19 '11 at 09:36
  • @EmptyStack that's a solution I thought of, but is it the best solution? Also, why is it only in iOS4 I'm getting this error? – colincameron Oct 19 '11 at 09:51
  • Sorry man! I've no idea about that. That's why I've just copied and pasted the link here ;-) – EmptyStack Oct 19 '11 at 10:09
  • @EmptyStack No bother mate, thanks for the link! – colincameron Oct 19 '11 at 10:11
  • 2
    I just came across this on SO and thought I'd add that the ability to auto generate scalar accessors is a new Core Data feature in iOS 5. It was mentioned at WWDC 2011 last year along with other new Core Data features in iOS 5, but the Core Data docs still have yet to be updated (and WWDC 2012 is just 2 weeks away). – Ziconic Jun 01 '12 at 17:47

1 Answers1

3

See here for information on using scalar attributes in core data. (By the way, NSInteger is a scalar). Listing 3 is the particular one you are interested in. Basically, you need to write your own accessor for it, but it's not difficult.

As of iOS5, you can use scalar properties in core data. This can be achieved by ticking the appropriate box when generating your managed object subclasses from the data model.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • That helps thanks. One question: I notice the example uses CGFloat, and you've mentioned NSInteger. It is better to use these rather than float, int, uint etc? – colincameron Oct 19 '11 at 10:15
  • CGFloat and NSInteger are cast to particular c types depending on the device you're compiling for (I think) - I'm not fully versed on the differences. I generally stick with NSInteger and CGFloat but I don't usually deal with very very big numbers so I don't think it makes a difference for me. See http://stackoverflow.com/questions/4445173/when-to-use-nsinteger-vs-int and http://stackoverflow.com/questions/1264924/whats-the-difference-between-using-cgfloat-and-float – jrturton Oct 19 '11 at 10:24
  • I see now. I've just done some reading on it and since all iOS devices use ARM cores, which are by definition 32-bit, does it really matter? I suppose at some point Apple could move away from ARM to a 64-bit architecture and my code wouldn't be optimised. – colincameron Oct 19 '11 at 11:18