I have multiple arrays, however, they are not retaining their data for use in another method.
Here's how I have it set up (simplified)
.h
NSArray *array;
@property (nonatomic, copy) NSArray *array;
-(void)someMethod:(NSArray*)someArray;
-(void)heresNewMethod;
.m
-(void)someMethod:(NSArray*)someArray
{
array = [someArray copy];
}
-(void)heresNewMethod //gets ca开发者_运维知识库lled by method not shown
{
NSLog(@"%@", array);
}
One of two things happened:
- You sent the object a
someMethod:
message, passingnil
(probably without meaning to). A message tonil
returnsnil
, so you assignednil
—as the result of thecopy
message—to thearray
instance variable. Even if you had stashed a pointer to an array there previously, you replaced it withnil
in your response to thissomeMethod:
message. - You never sent the object a
someMethod:
message. Since instance variables are initialized tonil
, and you never put anything different in thearray
instance variable, it still containsnil
.
Sprinkle more NSLog statements in your code to test the first theory. The truth is either one or the other, so confirming the first theory disproves the second, and vice versa.
Except for the fact that you'll leak whatever was in array
every time you call someMethod:
, that code should work. What's the problem you see?
The only answer is that the code you provided is not the code your using, and the difference is crucial. I mean, you declare a property which you then don't use, and it's not clear whether you are defining your accessors properly, or if array is also a local which is shadowing your property, or what.
Please post your real code.
精彩评论