NSMutableDictionary setValue:forKeyPath: setting more than one key [closed] - objective-c

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"]
];

Related

Objective-C: NSString.alloc.init, how's it look? [closed]

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.

Why Apple have heightForRowForIndexPath? [closed]

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 mean that looks like an overkill. It already has cellForRowForIndexPath
Once UITableViewSourceDelegate got the cell, it can easily got the height by computing the height of that cell.
So why?
heightForRowAtIndexPath is called for every row in the table in order to compute its total height and consequently set the correct size of the scroll indicator.
On the other hand cellForRowForIndexPath is called only for the visible cells.

Why NSArray does not have a firstObject method? [closed]

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!

Obj-C, trying to set accessability (including label) for the UIDatePicker? [closed]

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 haven't got a clue where to start and how far I can go.
I would like to add labels for individual values in the the date picker.
But I'm not sure if this is possible and how to do it ?
I haven't got a clue where to start
Always start with the documentation.

Obj-C method naming: setup method [closed]

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.
Imagine an ordinary UIViewController subclass. We want to do "setup stuff" to be preformed before -viewDidWhatever, but the problem is that there are three methods that could possible be called, either -initWithNibName, -initWithCoder or simply -init depending on how it is created.
The solution would be to create -setup to be called from all these three, but I'm a bit unsure about the name. My question to you is: is there any standard naming of this method?
I have commonly named it -setup myself when doing this.
Depending on what you want to do, you could put it in + (void)initialize, which is inherited from NSObject and will automatically be called before the class receives its first message.
Have a look here: Apple Developer Library: NSObject Class Reference