objective-c 在索引处将对象添加到目标 C 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1295208/
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-03 22:06:09  来源:igfitidea点击:

Adding Objects to an Objective C Array at Index

iphoneobjective-ccocoa-touchinsertnsmutablearray

提问by Bryan

I have a synthesized NSMutableArray - theResultArray . I want to insert NSNumber or NSInteger objects at specific indexes (0-49). For some reason I can never get any values to stick in my array. Every index returns nil or 0.

我有一个合成的 NSMutableArray - theResultArray 。我想在特定索引 (0-49) 处插入 NSNumber 或 NSInteger 对象。出于某种原因,我永远无法在我的数组中获得任何值。每个索引都返回 nil 或 0。

    NSInteger timeNum = time;
    [theResultArray insertObject:[NSNumber numberWithInt:timeNum] atIndex:rightIndex];
    NSLog(@"The right index is :%i", rightIndex);
    NSLog(@"The attempted insert time :%i", time);
    NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
    NSLog(@"The result of time insert is:%i", [testNum intValue]);

I alloc-init theResultsArray in viewDidLoad. Time is an integer. I have been trying different combinations of the code above to no avail.

我在 viewDidLoad 中分配初始化 theResultsArray。时间是一个整数。我一直在尝试上面代码的不同组合无济于事。

The console outputs this:

控制台输出这个:

StateOutlineFlashCards[20389:20b] The right index is :20
StateOutlineFlashCards[20389:20b] The attempted insert time :8
StateOutlineFlashCards[20389:20b] The result of time insert is:0

回答by peterb

Unless I'm misreading, aren't you inserting an NSInteger, but then trying to take out an NSNumber? Those are two completely different data types. It doesn't surprise me that you're getting odd results.

除非我误读了,你不是在插入一个 NSInteger,然后试图取出一个 NSNumber 吗?这是两种完全不同的数据类型。你得到奇怪的结果我并不感到惊讶。

Furthermore, NSInteger isn't an object, so you can't stick it into an array. You probably want to be allocating an NSNumber with that integer and putting that in.

此外, NSInteger 不是对象,因此您不能将其粘贴到数组中。您可能希望使用该整数分配一个 NSNumber 并将其放入。

Try something like: [theResultArray addObject:[NSNumber numberWithInteger:timeNum] atIndex:rightIndex];

尝试类似: [theResultArray addObject:[NSNumber numberWithInteger:timeNum] atIndex:rightIndex];

Likewise, when you retrieve the value, you'll need to unbox it:

同样,当您检索该值时,您需要将其拆箱:

NSLog(@"The result of time insert is:%i", [testNum integerValue])`;

Likewise, when you retrieve the value, you'll need to unbox it:

同样,当您检索该值时,您需要将其拆箱:

Frankly, I'm a little surprised that this even compiles.

坦率地说,我有点惊讶这甚至可以编译。

回答by Mark

You need to allocate memory for the array in your init or viewDidLoad method, otherwise you won't be able to store anything at all.

您需要在 init 或 viewDidLoad 方法中为数组分配内存,否则您将根本无法存储任何内容。

If you do this:

如果你这样做:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization        
        myMutableArrayName = [[NSMutableArray alloc] init];
    }
    return self;
}

Or this:

或这个:

- (void)viewDidLoad {
    [super viewDidLoad];
    myMutableArrayName = [[NSMutableArray alloc] init];
}

It should work for you.

它应该适合你。

As for storing integers in an NSMutableArray, I took a simple but somewhat "hackish" approach recently. I store them as strings. When I put them in I use:

至于在 NSMutableArray 中存储整数,我最近采用了一种简单但有点“hackish”的方法。我将它们存储为字符串。当我把它们放进去时,我使用:

[NSString stringWithFormat:@"%d", myInteger];

And when I take them out I convert with:

当我把它们拿出来时,我会转换成:

[[myArray objectAtIndex:2] intValue];

It was really easy to implement but depending on the context you may want to use another way.

它真的很容易实现,但根据上下文您可能想要使用另一种方式。

回答by mk12

NSInteger timeNum = time;

What is that for? What is "time"?

那是做什么用的?时间是什么”?

    [theResultArray addObject:timeNum atIndex:rightIndex];

There is no method -addObject:atIndex:. It is -insertObject:atIndex:. Why are you inserting at "rightIndex" anyway? Why not just use -addObject:?

没有方法 -addObject:atIndex:。它是 -insertObject:atIndex:。你为什么要插入“rightIndex”?为什么不直接使用 -addObject:?

    //[theResultArray replaceObjectAtIndex:rightIndex withObject:[NSNumber numberWithInt:timeNum]];

What is that and why is it commented out?

那是什么,为什么要注释掉?

    NSLog(@"The right index is :%i", rightIndex);
    NSLog(@"The attempted insert time :%i", time);
    NSNumber *testNum = [theResultArray objectAtIndex:rightIndex];
    //int reso = [testNum integerValue];
    NSLog(@"The result of time insert is:%i", testNum);

What are you trying to do?

你想做什么?