Ios __NSMutableArray中的NSCFString在分配给它的函数之外变为零
我的应用程序中有一个代表用户的类,Ios __NSMutableArray中的NSCFString在分配给它的函数之外变为零,ios,objective-c,arrays,json,nsstring,Ios,Objective C,Arrays,Json,Nsstring,我的应用程序中有一个代表用户的类,OtherUser。此类包含多个NSString*属性。当我使用静态@“string”字符串分配这些属性时,这些属性将另存为\uu NSCFConstantString*值。当我用JSON转换的字符串分配它们时,它们被保存为\uu NSCFString*值。我正在保存OtherUser对象的可变数组的可变数组。当我访问分配值的函数之外的数组时,只有\uuu NSCFConstantString*值保持不变,而\uu NSCFString*值都变为零。有没有办法将
OtherUser
。此类包含多个NSString*
属性。当我使用静态@“string”
字符串分配这些属性时,这些属性将另存为\uu NSCFConstantString*
值。当我用JSON转换的字符串分配它们时,它们被保存为\uu NSCFString*
值。我正在保存OtherUser
对象的可变数组的可变数组。当我访问分配值的函数之外的数组时,只有\uuu NSCFConstantString*
值保持不变,而\uu NSCFString*
值都变为零。有没有办法将我的NSCFString对象转换为NSCFConstantString对象,或者有没有办法绕过这个限制
是数组在我的ConnectiondFinishLoading方法中分配完所有值后的样子。用户测试Testman是使用下载的JSON数据创建的,而用户Rose Lalonde是使用@“string”
值在本地创建的
是在我的cellforrowatinexpath
方法中再次将其拉出时数组的样子。Testman会丢失除ID之外的所有数据,而Rose Lalonde会保留她的所有数据。(我为链接图像而不是嵌入而道歉-我的代表还不够高,无法嵌入图像)
下面是我的ConnectionDiFinishLoading方法,它首先被调用并创建数组:
//Called when the connection finishes loading all data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Connection finished!");
//Converts downloaded JSON data to an array
NSError *error = [[NSError alloc] init];
NSArray *arr = [NSJSONSerialization JSONObjectWithData:_responseData
options:kNilOptions
error:&error];
//Checks if the array was properly instantiated
if (!arr) {
NSLog(@"Error parsing JSON: %@", error);
} else {
//Initializes the arrays if they are not already initialized
if (!_bestFriends) _bestFriends = [[NSMutableArray alloc] init];
if (!_onlineFriends) _onlineFriends = [[NSMutableArray alloc] init];
if (!_offlineFriends) _offlineFriends = [[NSMutableArray alloc] init];
//Loops through each result dictionary and creates a friend object with its contents
for(NSDictionary *item in arr) {
OtherUser *friend = [[OtherUser alloc] init];
friend.firstname = [NSString stringWithString:(NSString *)[item objectForKey:@"firstname"]];
friend.lastname = (NSString *)[item objectForKey:@"lastname"];
friend.username = (NSString *)[item objectForKey:@"username"];
friend.userID = [NSNumber numberWithInt:[(NSString *)[item objectForKey:@"userID"] intValue]];
friend.isOnline = [(NSString *)[item objectForKey:@"isOnline"] boolValue];
friend.isInSession = [(NSString *)[item objectForKey:@"isInSession"] boolValue];
//Sorts the friend into online or offline
if (friend.isOnline) {
[_onlineFriends addObject:friend];
} else {
[_offlineFriends addObject:friend];
}
}
//user object created with local data to test best friends array
OtherUser *tempUser = [[OtherUser alloc] initWithFirstname:@"John"
Lastname:@"Egbert"
Username:@"ectobiologist"
UserID:[NSNumber numberWithInt:10]
OnlineState:YES
SessionState:NO];
[_bestFriends addObject:tempUser];
//User created with local data to compare to downloaded JSON users
OtherUser *tempUser2 = [[OtherUser alloc] initWithFirstname:@"Rose"
Lastname:@"Lalonde"
Username:@"tentacletherapist"
UserID:[NSNumber numberWithInt:15]
OnlineState:YES
SessionState:NO];
[_onlineFriends addObject:tempUser2];
}
//Adds the arrays to the array array
self.arrays = [NSMutableArray arrayWithObjects:_bestFriends, _onlineFriends, _offlineFriends, nil];
//Resets the table data
[self.tableView reloadData];
NSLog(@"reloading data...");
}
我的CellForRowatingIndexPath方法(下一个要调用的方法)是擦除数组数据的地方,它看起来如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Creating Cell...");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//Creates a user object from the proper index of the proper array (e.g. _onlineFriends[1])
OtherUser *friend = [[_arrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
// Configure Cell
UILabel *label = (UILabel *)[cell.contentView viewWithTag:10];
[label setText:[friend fullname]];
NSLog(@"Cell created!");
return cell;
}
和我的FriendsTableViewController.h文件,以防万一:
@interface FriendsTableViewController : UITableViewController <NSURLConnectionDelegate> {
NSMutableData *_responseData;
NSMutableArray *_onlineFriends;
NSMutableArray *_bestFriends;
NSMutableArray *_offlineFriends;
}
@property NSMutableArray *arrays;
@interface FriendsTableViewController:UITableViewController{
NSMutableData*_responseData;
NSMutableArray*_onlineFriends;
NSMutableArray*_最佳朋友;
NSMutableArray*_离线好友;
}
@属性NSMutableArray*数组;
编辑:其他用户文件:
//OtherUser.h
#import <Foundation/Foundation.h>
@interface OtherUser : NSObject
@property (weak, nonatomic) NSNumber *userID;
@property (weak, nonatomic) NSString *firstname;
@property (weak, nonatomic) NSString *lastname;
@property (weak, nonatomic) NSString *username;
@property (nonatomic, assign) BOOL isOnline;
@property (nonatomic, assign) BOOL isInSession;
@property (weak, nonatomic) NSNumber *sessionID;
- (id)initWithFirstname:(NSString *)firstName Lastname:(NSString *)lastName
Username:(NSString *)userName UserID:(NSNumber *)uID
OnlineState:(BOOL)online SessionState:(BOOL)inSession;
- (id) init;
- (NSString *) fullname;
//OtherUser.m
//Initializes an empty User object
- (id)init {
self = [super init];
if (self) {
self.userID = 0;
self.username = @"";
self.firstname = @"";
self.lastname = @"";
self.isOnline = NO;
self.isInSession = NO;
return self;
}
return nil;
}
- (id)initWithFirstname:(NSString *)firstName Lastname:(NSString *)lastName
Username:(NSString *)userName UserID:(NSNumber *)uID
OnlineState:(BOOL)online SessionState:(BOOL)inSession {
self = [super init];
if (self) {
self.userID = uID;
self.username = userName;
self.firstname = firstName;
self.lastname = lastName;
self.isOnline = online;
self.isInSession = inSession;
NSLog(@"Friend %@ %@ created.", self.firstname, self.lastname);
return self;
}
return nil;
}
- (NSString *)fullname {
return [NSString stringWithFormat:@"%@ %@", self.firstname, self.lastname];
}
//OtherUser.h
#进口
@接口其他用户:NSObject
@属性(弱,非原子)NSNumber*用户ID;
@属性(弱,非原子)NSString*firstname;
@属性(弱,非原子)NSString*lastname;
@属性(弱,非原子)NSString*用户名;
@属性(非原子,赋值)布尔等值线;
@属性(非原子,赋值)布尔值;
@属性(弱,非原子)NSNumber*sessionID;
-(id)initWithFirstname:(NSString*)firstName Lastname:(NSString*)Lastname
用户名:(NSString*)用户名用户ID:(NSNumber*)uID
在线状态:(BOOL)在线会话状态:(BOOL)插入;
-(id)init;
-(NSString*)全名;
//OtherUser.m
//初始化空的用户对象
-(id)init{
self=[super init];
如果(自我){
self.userID=0;
self.username=@;
self.firstname=@”;
self.lastname=@”;
self.isOnline=否;
self.isInSession=否;
回归自我;
}
返回零;
}
-(id)initWithFirstname:(NSString*)firstName Lastname:(NSString*)Lastname
用户名:(NSString*)用户名用户ID:(NSNumber*)uID
在线状态:(BOOL)在线会话状态:(BOOL)插入{
self=[super init];
如果(自我){
self.userID=uID;
self.username=用户名;
self.firstname=firstname;
self.lastname=lastname;
self.isOnline=在线;
self.isInSession=插入;
NSLog(@“Friend%@%@created.”,self.firstname,self.lastname);
回归自我;
}
返回零;
}
-(NSString*)全名{
返回[NSString stringWithFormat:@“%@%@”,self.firstname,self.lastname];
}
您已将“firstname”、“lastname”和其他属性声明为弱属性:
这意味着它们不保留引用的对象,并将设置为nil
如果引用的对象已解除分配。当JSON数组arr
停止时会发生这种情况
超出范围NSString
literals是静态分配的,从不释放,
这就是为什么罗斯·拉隆德不会在这里失去财产
您应该将Objective-C属性声明为strong,或者(对于字符串)声明为copy:
顺便说一句,所有类型强制转换和
stringWithString
调用
friend.firstname = [NSString stringWithString:(NSString *)[item objectForKey:@"firstname"]];
friend.lastname = (NSString *)[item objectForKey:@"lastname"];
// ...
如果没有必要,您可以将其简单地写为
friend.firstname = [item objectForKey:@"firstname"];
friend.lastname = [item objectForKey:@"lastname"];
或者,使用(较新的)下标符号:
friend.firstname = item[@"firstname"];
friend.lastname = item[@"lastname"];
其中添加了OtherUser.h/.m。我一开始应该包括它,抱歉。属性(弱,非原子)-这就解释了。是的,弱引用就是这样。哇,我现在觉得代码非常聪明。谢谢,我确信我遇到了更严重的障碍。
friend.firstname = [item objectForKey:@"firstname"];
friend.lastname = [item objectForKey:@"lastname"];
friend.firstname = item[@"firstname"];
friend.lastname = item[@"lastname"];