简体   繁体   中英

how can I retrieve an integer value from an NSMutableArray on the iPhone?

如何从NSMutableArray中获取整数值?

You'll need to use NSNumber :

myArray = [NSMutableArray array];
[myArray addObject:[NSNumber numberWithInteger:1234]];

//..

int theNumber = [[myArray objectAtIndex:0] integerValue];

You have this extends if you want

Create file extends.h add this code and #import "extends.h" in your project :

/*______________________________ Extends NSMutableArray ______________________________*/
/**
 * Extend NSMutableArray
 * By DaRkD0G
 */
@interface NSMutableArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index;
@end
/**
 * Extend NSMutableArray Method
 * By DaRkD0G
 */
@implementation NSMutableArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index {
    NSInteger anIndex = index;
    NSObject *object = [self objectAtIndex:anIndex];
    if (object == [NSNull null]) {
        return nil;
    } else {
        NSLog(@"OK found ");
        return object;
    }

}
@end
/*______________________________ Extends NSArray ______________________________*/
/**
 * Extend NSArray
 * By DaRkD0G
 */
@interface NSArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index;
@end
/**
 * Extend NSArray Method
 * By DaRkD0G
 */
@implementation NSArray (NSArrayAdd)
/**
 *  Get element at index
 *
 *  @param index
 */
- (NSObject *) getAt:(int) index {
    NSInteger anIndex = index;
    NSObject *object = [self objectAtIndex:anIndex];
    if (object == [NSNull null]) {
        return nil;
    } else {
        NSLog(@"OK found ");
        return object;
    }

}
@end

USE :

NSObject object = [self.arrayItem getAt:0];
NSObject object2 = [self.arrayItem getAt:50];

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