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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/939549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:35:37  来源:igfitidea点击:

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

objective-ciphonecocoa-touchxcode

提问by Rahul Vyas

How would I get an integer value out of an NSMutableArray?

如何从 NSMutableArray 中获取整数值?

回答by e.James

You'll need to use NSNumber:

你需要使用NSNumber

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

//..

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

回答by YannSteph

You have this extends if you want

如果你愿意,你有这个扩展

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

创建文件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

USE :

用 :

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