Objective-C的新文字语法可以模仿addObject吗?

时间:2021-07-31 22:26:10

I know that I can do this to :

我知道我可以这样做:

NSMutableArray *objects = [@[objectOne, objectTwo] mutableCopy];
NSObject *someObject = [NSObject new];
objects[0] = someObject;

But is there a way for the new literal syntax to mimic addObject:?

但有没有办法让新的文字语法模仿addObject:?

1 个解决方案

#1


13  

As I was writing this, I tried out this bit of code and it worked like a charm. It isn't exactly documented, but I think this is a safe way to handle addObject: with the new literal syntax.

在我写这篇文章时,我尝试了这段代码,它就像一个魅力。它没有完全记录,但我认为这是一种处理addObject的安全方法:使用新的文字语法。

NSMutableArray *objects = [@[] mutableCopy];
NSObject *someObject = [NSObject new];
objects[objects.count] = object;

#1


13  

As I was writing this, I tried out this bit of code and it worked like a charm. It isn't exactly documented, but I think this is a safe way to handle addObject: with the new literal syntax.

在我写这篇文章时,我尝试了这段代码,它就像一个魅力。它没有完全记录,但我认为这是一种处理addObject的安全方法:使用新的文字语法。

NSMutableArray *objects = [@[] mutableCopy];
NSObject *someObject = [NSObject new];
objects[objects.count] = object;