简体   繁体   中英

How to get last array object as int

How can I get the last object from an array as an int, for example if I have 20 objects in the array, how can I get the last object as an int variable that = 20. Or if the array had 999 objects, the last object int would = 999.

How can I do this? I have tried using (int)array.lastobject but have had no luck, any help would be greatly appreciated.

It depends on what type of objects are being stored in the array? if they are NSNumber or NSString than you can do this:

int myVar = [[array lastObject] intValue];

Edit: Your question is rather confusing it also seems that you want array count instead of last object? If thats the case than use this:

int myVar = [array count];

Option 1

int yourVariable = [[yourArray lastObject] intValue];

Option 2

int yourVariable = [[yourArray objectAtIndex:([yourArray count]-1)] intValue];

您可以使用lastObject方法从数组中获取最后一个对象

 int lastValue = [[yourArray lastObject] intValue];

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