xcode 如何将一个 NSMutablearray 复制到另一个 NSMutablearray?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8953171/
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
How to copy one NSMutablearray to another NSMutablearray?
提问by MayurCM
I know that you guys may feel that this is repetitive question. But I'm screwed up with this issue with my project.
Now moving to the issue, I have taken an NSMutableArray
named arr1
that contains string values such as @"34"
, @"40"
and so on. Now, I have second NSMutableArray
named arr2
. I have already tried below code.
我知道你们可能会觉得这是一个重复的问题。但是我在我的项目中被这个问题搞砸了。现在转到这个问题,我采用了一个包含字符串值的NSMutableArray
命名arr1
,例如@"34"
,@"40"
等等。现在,我有第二个NSMutableArray
名字arr2
。我已经尝试过下面的代码。
[arr1 initWithObjects:@"1",@"2", nil];
arr2 = [[[NSMutableArray alloc] initWithArray:arr1 copyItems:YES] autorelease];
Then, I tried
然后,我试过了
NSString *no = [arr2 objectAtIndex:0];
tst.text = no;
However, it gives me no value in my textbox.
If any1 can help me out with this would be great help.
但是,它在我的文本框中没有任何价值。
如果 any1 可以帮助我解决这个问题,那将是很大的帮助。
回答by zaph
[arr1 initWithObjects:@"1",@"2", nil];
makes no sense, what you probably want is to use the NSMutableArray
:
没有意义,您可能想要的是使用NSMutableArray
:
NSArray *arr1 = [NSArray arrayWithObjects:@"1",@"2", nil];
NSMutableArray *arr2 = [arr1 mutableCopy];
NSLog(@"arr1: %p, %@", arr1, arr1);
NSLog(@"arr2: %p, %@", arr2, arr2);
NSLog output:
NSLog 输出:
arr1: 0x7924c50, (
1,
2
)
arr2: 0x7924f00, (
1,
2
)
回答by tia
Your arr1 might be nil. It should be
您的 arr1 可能为零。它应该是
arr1 = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
回答by Paul.s
Your arr1
must be nil
otherwise you would crash, but more to the point you are creating the NSMutableArray
incorrectly:
你arr1
必须是nil
否则你会崩溃,但更重要的是你正在NSMutableArray
错误地创建:
You should not really do alloc
and init
on separate lines
你不应该真正做到alloc
并init
在不同的行
NSMutableArray *myArray = [NSMutableArray alloc];
NSLog(@"%@", [myArray class]);
//=> __NSPlaceholderArray
In this case the myArray
pointer is pointing at a flyweight of type __NSPlaceholderArray
which is most likely not what you want. You would prefer:
在这种情况下,myArray
指针指向的享元类型__NSPlaceholderArray
很可能不是您想要的。你更喜欢:
---------------------------------------
myArray = [[NSMutableArray alloc] init];
NSLog(@"%@", [myArray class]);
//=> __NSArrayM
You will also crash if you do have a legitimate NSMutableArray
and you call initWithArray:
with:
如果你有一个合法的NSMutableArray
并且你打电话给你,你也会崩溃initWithArray:
:
[NSMutableArray initWithCapacity:]: method only defined for abstract class.
This is because the method is most likely defined on __NSPlaceholderArray
这是因为该方法很可能定义在 __NSPlaceholderArray
回答by Raphael Petegrosso
Are you trying [arr2 objectAtIndex:0]
in the same method where arr2
is being created?
您是否尝试[arr2 objectAtIndex:0]
使用与arr2
创建相同的方法?
Maybe the problem is that you are using autorelease
on your arr2
. So, in other methods it may have lost its values.
也许问题在于您autorelease
在arr2
. 所以,在其他方法中,它可能已经失去了它的价值。
I tried the following and it works for me:
我尝试了以下方法,它对我有用:
NSMutableArray * arr1 = [[NSMutableArray alloc]initWithObjects:@"1", @"2", nil];
NSMutableArray * arr2 = [[[NSMutableArray alloc] initWithArray:arr1 copyItems:YES] autorelease];
NSString * no = [arr2 objectAtIndex:0];
NSLog(@"%@", no);
回答by Jodocus
In addition to tia's answer: you need to allocate it first.
除了tia的回答:你需要先分配它。
The method 'mutableCopy' will create a copy of the array, not of the elements inside it. You could create a new immutable array with 'initWithArray:arr1 copyItems:YES' and subsequently create a mutable version of it by calling 'mutableCopy'.:
'mutableCopy' 方法将创建数组的副本,而不是其中的元素的副本。您可以使用 'initWithArray:arr1 copyItems:YES' 创建一个新的不可变数组,然后通过调用 'mutableCopy' 创建它的可变版本。:
NSArray *arr1 = [[NSArray alloc] initWithObjects:@"1",@"2", nil];
NSMutableArray arr2 = [[[NSArray alloc] initWithArray: arr1 copyItems:YES] mutableCopy];