如何从iPhone上的NSMutableArray中检索整数值?

时间:2009-06-02 13:30:54

标签: objective-c iphone cocoa-touch xcode

如何从NSMutableArray中获取整数值?

2 个答案:

答案 0 :(得分:17)

您需要使用NSNumber

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

//..

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

答案 1 :(得分:0)

如果你想要

,你可以扩展

创建文件 extends.h 在您的项目中添加此代码和 #import“extends.h”

/*______________________________ 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

使用

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