【发布时间】:2014-04-03 13:53:57
【问题描述】:
我想在整个类 (self.array) 中使用类似 NSArray 的属性,但我不知道怎么做。现在我的 LoginVC 中有这段代码:
LoginVC.h:
#import <UIKit/UIKit.h>
@interface LoginVC : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;
@property (strong, nonatomic) IBOutlet UIScrollView *loginButton;
@property (nonatomic, strong) NSArray *allUsers;
@end
LoginVC.m:
- (void)viewDidLoad
{
if (debug == 1) {
NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
}
[super viewDidLoad];
// Do any additional setup after loading the view.
UserController *userController = [[UserController alloc]init];
[userController getUsers];
self.allUsers = [NSArray new];
self.allUsers = [userController getUsers];
}
getUser 方法返回一个 NSArray,但是这个解决方案不起作用。 llbd 报错:*Thread 1: EXC_BAD_ACCESS (code=2, address=0x38)*
我认为问题在于初始化数组和赋值。
getUsers 声明:
-(NSArray *)getUsers{
CoreDataHelper *cdh = [(AppDelegate *)[[UIApplication sharedApplication]delegate] cdh];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
NSArray *users = [cdh.context executeFetchRequest:request error:nil];
return users;
}
“getUsers”方法工作正常。
这是错误的屏幕。它的强度,因为 self.allUsers 包含一个用户名为“admin”的对象,而且它绝对正确。
https://www.dropbox.com/s/ui5rtq9ov0p2xx9/Screenshot%202014-03-02%2019.24.53.png
我认为问题出在数组中,因为当我删除分配给数组的代码时。错误消失。
【问题讨论】:
-
发布
getUsers方法,或至少发布相关部分。 -
Objective-C 和 iOS 编程的任何介绍都涵盖了这类内容。
-
你的财产声明没问题。
-
self.allUsers = [NSArray new];毫无意义,因为您会立即用另一个数组覆盖该数组。但这不会导致这个问题 - 你可以发布你的整个堆栈跟踪吗?为什么你认为这个错误与这个数组有关?
标签: ios initialization nsarray exc-bad-access lldb