Objective c 具有现代语法的可变数组的可变数组
如何编写以下代码:Objective c 具有现代语法的可变数组的可变数组,objective-c,nsarray,Objective C,Nsarray,如何编写以下代码: self.box = [[NSMutableArray alloc] initWithObjects: [NSMutableArray arrayWithObjects:_imageView1,_imageView2,_imageView3,nil], [NSMutableArray arrayWithObjects:_imageView4,_imageView5,_imageView6,nil]
self.box = [[NSMutableArray alloc] initWithObjects:
[NSMutableArray arrayWithObjects:_imageView1,_imageView2,_imageView3,nil],
[NSMutableArray arrayWithObjects:_imageView4,_imageView5,_imageView6,nil],
[NSMutableArray arrayWithObjects:_imageView7,_imageView8,_imageView9,nil],
nil];
使用现代语法?我将自我回答:对于NSMutableArray,没有文字语法,因此您必须编写:
self.box = [@[
[@[ _imageView1, _imageView2, _imageView3 ] mutableCopy],
[@[ _imageView4, _imageView5, _imageView6 ] mutableCopy],
[@[ _imageView7, _imageView8, _imageView9 ] mutableCopy]
] mutableCopy];
我将自我回答:对于NSMutableArray,没有文字语法,因此您必须编写:
self.box = [@[
[@[ _imageView1, _imageView2, _imageView3 ] mutableCopy],
[@[ _imageView4, _imageView5, _imageView6 ] mutableCopy],
[@[ _imageView7, _imageView8, _imageView9 ] mutableCopy]
] mutableCopy];
如果您希望使用比您自己的答案更少的括号,您可以使用:
self.box = @[
@[_imageView1, _imageView2, _imageView3].mutableCopy,
@[_imageView4, _imageView5, _imageView6].mutableCopy,
@[_imageView7, _imageView8, _imageView9].mutableCopy
].mutableCopy;
如果您希望使用比您自己的答案更少的括号,您可以使用:
self.box = @[
@[_imageView1, _imageView2, _imageView3].mutableCopy,
@[_imageView4, _imageView5, _imageView6].mutableCopy,
@[_imageView7, _imageView8, _imageView9].mutableCopy
].mutableCopy;
你真的需要可变数组吗?UI元素不太可能。你真的需要可变数组吗?对于UI元素来说,这不太可能。我在这里不会使用点符号,因为mutableCopy是一个普通的方法,而不是一个属性:-(id)mutableCopy;虽然它较短,但可能会导致混淆代码,如:“someObject.someReallyExpensiveMethod”和“NSMutableArray.alloc.init.count”。@MaartenFoukhar-我几乎在回答中写道,有些人可能不喜欢这样,因为
mutableCopy
被声明为具有类似getter属性类型的方法,但决定不-不想让敏感的斯威夫特人不高兴;-)撇开玩笑不谈,我认为他们的关键词是这样的,是的,它可以被滥用,如果走到极端,但这个案件没有。(我承认我也经常写.new
,但从来没有写过.alloc.init
!)我不会在这里使用点符号,因为mutableCopy是一个普通的方法,而不是属性:-(id)mutableCopy;虽然它较短,但可能会导致混淆代码,如:“someObject.someReallyExpensiveMethod”和“NSMutableArray.alloc.init.count”。@MaartenFoukhar-我几乎在回答中写道,有些人可能不喜欢这样,因为mutableCopy
被声明为具有类似getter属性类型的方法,但决定不-不想让敏感的斯威夫特人不高兴;-)撇开玩笑不谈,我认为他们的关键词是这样的,是的,它可以被滥用,如果走到极端,但这个案件没有。(我承认我也经常写.new
,但从未写过.alloc.init
!)