NSArray LastObject - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 4, 2012, 10:54 PM   #1
larswik
macrumors 65816
 
Join Date: Sep 2006
NSArray LastObject

Hey quick question. I have a plist that I am unpacking to add an object and re save.

Code:
 NSMutableArray *exerArray = [[NSMutableArray alloc] initWithContentsOfFile:[bpc returnPath:@"exerciseLog.plist"]];
    NSMutableDictionary *exerDict = [NSMutableDictionary dictionaryWithDictionary:[exerArray lastObject]];
    [exerDict setValue: totalCalLabel.text forKey: itemNameTextField.text];
It's the objects I need will always be at the last index so I can use "LastObject" Task from the Array class to perform that. But when I add an object to a dict and want to replace the dict that is saved at the last index of the Array I need the index number to replace that index.

The way I have been doing it is to just get the count of the Array then -1 to find the last index to write to. I was just wondering if anyone new a short cut that I can write to the lastObject just as easy as I retrieve from it.

I am not stuck and my method works just fine, I was just wondering?

Thanks
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Nov 5, 2012, 06:33 AM   #2
Reason077
macrumors 65816
 
Join Date: Aug 2007
Quote:
Originally Posted by larswik View Post
It's the objects I need will always be at the last index so I can use "LastObject" Task from the Array class to perform that. But when I add an object to a dict and want to replace the dict that is saved at the last index of the Array I need the index number to replace that index.

The way I have been doing it is to just get the count of the Array then -1 to find the last index to write to. I was just wondering if anyone new a short cut that I can write to the lastObject just as easy as I retrieve from it.

I am not stuck and my method works just fine, I was just wondering?
This is perfectly correct. lastObject is just a shortcut for "objectAtIndex: ([array count] - 1)".
Reason077 is offline   0 Reply With Quote
Old Nov 5, 2012, 02:40 PM   #3
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Thanks Reason0777, I am always looking for little short cuts in code when possible. Since I saw the "lastObject" it got me wondering if there was the opposite of that.

It seems like Apple should make a change to "lastObject" and make Task that is "lastIndex" instead. This would allow you to read or replace to the last index of an array.

But thanks, I was just wondering!
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Nov 5, 2012, 05:19 PM   #4
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
replaceObject:atIndex: should do what you want. If you want something like replaceLastObjectWithObject: you can easily add that with a category.
PhoneyDeveloper is offline   0 Reply With Quote
Old Nov 5, 2012, 07:01 PM   #5
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Yep, that is how I am currently replacing it. I have read about Categories but I have never used them. Might be a good learning experience for that. replaceObject:atIndex: required knowing the index number. Where as retrieving the last object you do not need the index number. If you could do lastObject.count in [myArray replaceObject: newObject atIndex: lastObject.count]; , although you can't of course.

It is just speculation and I don't mean to start a discussion on the subject. But I think learning / implementing categories could be a fun project for tonight.

Thanks again!
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
XCode thinks that an NSMutableArray is an NSArray BadWolf13 Mac Programming 2 May 25, 2014 01:35 PM
NSArray to NSMutableArray larswik Mac Programming 3 Feb 19, 2013 10:10 PM
Adding NSNumbers in a NSArray fstigre iPhone/iPad Programming 4 Nov 11, 2012 11:46 AM
What is the difference between NSArray and NSMutableArray anandkumar45 iPhone Tips, Help and Troubleshooting 1 Nov 2, 2012 05:09 AM
(NSArray)writeToFile: issue mdeh Mac Programming 3 Jul 15, 2012 06:38 PM

Forum Jump

All times are GMT -5. The time now is 02:22 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC