gpt4 book ai didi

ios - NSMutableArray 中的 __NSCFString 在分配它的函数之外变为 nil

转载 作者:行者123 更新时间:2023-12-01 18:57:34 25 4
gpt4 key购买 nike

我的应用程序中有一个代表用户的类 OtherUser .这个类包含几个 NSString *特性。当我用静态 @"string" 分配这些属性时字符串,属性保存为__NSCFConstantString *值(value)观。当我为它们分配从 JSON 转换的字符串时,它们被保存为 __NSCFString *值(value)观。我正在保存 OtherUser 的可变数组的可变数组对象。当我在分配了值的函数之外访问数组时,只有 __NSCFConstantString *值仍然存在,而 __NSCFString *值全部变为 nil。有没有办法将我的 NSCFString 对象转换为 NSCFConstantString 对象,或者有什么方法可以解决这个限制?

This是在我的 ConnectionDidFinishLoading 方法中分配了所有值后数组的样子。用户测试 Testman 是使用下载的 JSON 数据创建的,而用户 Rose Lalonde 是使用 @"string" 在本地创建的值(value)观。

This当我在我的 CellForRowAtIndexPath 中再次拉起它时,该数组的样子是什么?方法。 Test Testman 丢失了除 ID 之外的所有数据,而 Rose Lalonde 保留了她的所有数据。 (我为链接图像而不是嵌入而道歉 - 我的代表还不足以嵌入图像)。

这是我的 connectionDidFinishLoading 方法,首先调用它并创建数组:

//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...");
}

我的 CellForRowAtIndexPath 方法,接下来将被调用并且是数组数据被删除的地方,如下所示:
- (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;

编辑:其他用户文件:
//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];
}

最佳答案

您已将“名字”、“姓氏”和其他属性声明为弱:

@property (weak, nonatomic) NSString *firstname;

这意味着它们不保留引用的对象,并将设置为 nil如果引用的对象被释放。当 JSON 数组 arr
超出范围。 NSString文字是静态分配的,永远不会被释放,
这就是为什么 Rose Lalonde 不会失去这里的属性。

您应该将 Objective-C 属性声明为 strong,或者 - 在字符串的情况下 - 作为副本:
@property (copy, nonatomic) NSString *firstname;
// ...
@property (strong, nonatomic) NSNumber *sessionID;

顺便说一句,所有类型转换和 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"];

关于ios - NSMutableArray 中的 __NSCFString 在分配它的函数之外变为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25578396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com