简体   繁体   中英

NSMutableArray lastObject not working in UITableView iPhone?

I want to get the last object from an NSMutableArray . When I use the code below to get the last object from the NSMutableArray it is not finding it:

if ([mutableArray lastObject])
{
  NSLog(@"Last Object");
}
else
{
  NSLog(@"Not a Last Object");
}

When ever I call this code the if statement is only calling for every objects from an array. Can anyone please help me to solve this?

[mutableArray lastObject] returns an id typed object which is the last object of your array. it is not a test.

To see it you should write something like :

id myLastObject = [mutableArray lastObject];
NSLog(@"my last object is : %@",myLastObject);

edit : What do you mean by check last object ?

checking equality with the isEqual method checks if both objects are same objects, meaning only one object but you might wanna check if two objects have same properties, thus checking if properties have same values.

if ([someObject isEqual:[mutableArray lastObject]]) {

      NSLog(@"Last Object");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM