As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
But it does have a lastObject, anybody know why?
My guess is because lastObject reduces more boilerplate code. You use [array lastObject] to replace either [array objectAtIndex:array.count - 1] or array[array.count - 1] using modern Objective-C syntax.
Whereas in the case of firstObject you can simply check [array objectAtIndex:0] or array[0].
It just helps streamline things to be able to call lastObject instead of typing out that function.
Update
As #Nathaniel Symer suggested in his comment above, firstObject has previously been available but only in private API (I believe since iOS 4). However, as of the release of the iOS 7 SDK, firstObject is now publicly available!
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
OK, this is pretty much what I'm trying to do.
I've got a HUGE static library full of functions, which has to be NON-ARC (because of errors).
Now my app has to be ARC-enabled.
However : when ever I'm trying to compile my app, it throws all errors related to my using the NON-arc library.
Any ideas on how this can be solved?
Combining ARC with MRC is not at all a problem. In fact, you do it every day, as many system frameworks are not implemented using ARC, yet.
So, what are your errors?
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
The syntax is legal. It'll save you about 2 seconds every time you alloc/init an object. Do you think this could be a new trend?
Again,
NSObject *obj = NSObject.alloc.init;
[NSObject new];
Saves even more and doesn't cause any religious wars.
Yes, it saves a few keystrokes, but I doubt it will catch on. See this blog post for a number of cases where using dot notation on methods rather than properties will not work. I think most people view this as a side effect of ObjC2.
Furthermore, I don't think it enhances readability at all. As soon as you want to send a message with an argument, you have to revert back to the original syntax.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm learning this from a 'newish' book, but I'm wondering what the current standard is as to the use of the dot operator for things outside of setting property values.
As in method calls and whatnot. Obviously myClass.myMethod:value is syntactically correct, but is it an accepted norm nowadays?
The dot operator is much more 'human' feeling than [myClass myMethod:myValue] in my opinion.
Have you tried compiling this? Your example of myClass.myMethod:value is not valid as far as I understand it.
The dot operator is translated by the compiler to either -(void)setMyValue:(ValueType*) or -(ValueType*)myValue depending on if you are getting or setting it.
Read the apple documentation for more info:
http://developer.apple.com/library/ios/documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW17
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
// populate 'project' with contents of key in `gDictRoot`
NSDictionary *project = [gDictRoot valueForKeyPath:#"root.project0"];
// modify 'project' as necessary - actual code omitted for brevity
[project setValue:[someDict valueForKey:#"foo"] forKeyPath:#"parameters.foo"];
// add 'project' to 'gDictRoot' so it isn't lost when the view is dismissed
[gDictRoot setValue:project forKeyPath:#"root.project2"];
Third line, where I add project to gDictRoot – project0 also gets modified. Don't know why.
The behavior that you see is due to the fact that both project0 and project2 point to the same dictionary instance. The change to one of them will always reflect in the other one.
If you do not want this behavior, make a copy of project0 before making it project2:
NSMutableDictionary *project = [NSMutableDictionary
dictionaryWithDictionary:[gDictRoot valueForKeyPath:#"root.project0"]
];
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
To better understand ARC I'm looking for example code that compiles perfectly with ARC enabled , but crashes at runtime. Ie the common pitfalls that may be overlooked and can cause you a debugging nightmare if you've never encountered that issue before.
Real life examples tuned down to the minimum code that reproduces the issues would be very helpful. In particular if the ARC code is interfacing with C or C++ code.
Quick example of the many I was thinking along the same lines as bbum.
Casting from CF... to NS... confuses ARC if done incorrectly for example:
CFArrayRef *supportedInterfaces = CNCopySupportedInterfaces();
NSArray *interfaceNames = (__bridge_transfer NSArray *)supportedInterfaces;
CFRelease(supportedInterfaces);
Would over-release supportedInterfaces since __bridge_transfer retains the NSArray while releasing the CFArrayRef. In this case either don't use the CFRelease() or use plain __bridge.