- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序中有一个代表用户的类 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...");
}
- (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;
}
@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
文字是静态分配的,永远不会被释放,
@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/
在检查对象是否为 nil 时,有人使用 1: if (object == nil) { //... } 有人用 2: if (nil == object) { //... } 1和2有
func InsertApData(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body)
为什么 nil.to_s 返回 "",而 nil.inspect 返回 "nil"(当显然 .inspect 使用 .to_s 方法) 最佳答案 "inspect" method of the Obj
运行时间 https://play.golang.org/p/sl12vfS9vP package main import "fmt" func main() { err := run()
我可以将三元条件运算符用于 if {} else {} 语句,如下所示:a ? x : y,还是问题?回答 1 : 回答 2。 是否可以使用这种格式来检查,而不是 a 是 true 还是 false,
无法弄清楚这里出了什么问题。按照设计设置说明,用谷歌搜索我能想到的一切,仍然没有运气。 undefined method `email' for # Extracted source (around
今天写一些 rspec 时,我遇到了一些意外的行为,将日期(和时间)实例与 nil 进行比较。这是一个使用原始 ruby 的示例(没有 Rails 或其他库): user@MacBook-Work
我将数据类型的非零值分配给非可选属性,然后将其分配给可选属性,最后用所述数据实例化图像。当可选项通过 if-let 子句时,它的 block 执行,抛出错误: Fatal error: Unexpec
swift 5.1 。 考虑以下。 let x: Any? = nil let y: Any = x print("x \(x)") // x nil pri
请耐心听我解释这一点, 我正在创建一个聊天室,用户可以在其中上传照片供其他人查看。当用户点击图标时,他们可以将照片上传到我的 Firebase 数据库(这已成功完成,我已经对此进行了测试)。照片的 U
我的 xCode 5.0 目前遇到一个奇怪的问题:一个对象在控制台中似乎为 nil,但同时它可以通过代码访问。 图 1:对象似乎为零 图2:NSLog(@"%@", imgDownloader) 的输
我有一个实现协议(protocol)的类,以便添加 3 个变量。我专门设置了图像变量,调试器显示该变量存在,但是在我打印它时在代码中显示为 nil,我的 if let 语句也认为该变量为 nil。 @
这个问题在这里已经有了答案: Swift 2 ( executeFetchRequest ) : error handling (5 个答案) 关闭 7 年前。 开始在 SWIFT 中学习编码,每次
两者 (not 'nil) 和 (not nil) 求值为T,那么'nil和nil有什么区别吗?那么 ''nil 呢?如果 ''nil 的计算结果为 'nil,那么 ''nil 是否也应该计算为 ni
(if '(nil nil) 'print-true 'print-false) (if '(nil) 'print-true 'print-false) 在上面的代码
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: obj.nil? vs. obj == nil 我发现了一个问题 - == nil 和 nil 哪个更好?
此代码段抛出异常: x = nil jsoned = x.to_json puts 'x.to_json=' + jsoned.inspect puts 'back=' + JSON.parse(js
基本上我有一个对象想要传递给前端。我在后端记录了它,它不是空的,但是在前端,当我提醒它时,它变成了空。 ... presentation := &presentationStruct { Obje
我创建了一个自定义错误类型来包装错误,以便在 Golang 中更轻松地进行调试。当有错误要打印时它可以工作,但现在它会引起 panic 。 Demo type Error struct { E
写一个符合我当前问题的标题有点难..我有一个 main() 函数,它使用另一个包 (database_sql) 中的函数。该函数初始化一个全局变量 sql.DB*。初始化后,变量不为nil,但是对于其
我是一名优秀的程序员,十分优秀!