Uitableview 如何从tableviewcontroller在另一个对象中设置integer属性

Uitableview 如何从tableviewcontroller在另一个对象中设置integer属性,uitableview,exc-bad-access,Uitableview,Exc Bad Access,我是objective C的新手,我正在尝试…尝试在tableviewcontroller代码中设置模型对象的integer属性。以下是模型对象项目的顶部。h: #import <Foundation/Foundation.h> @interface Project : NSObject @property (nonatomic) NSInteger *selecto; @end 我已使用app委托的didFinishLaunchingWithOptions方法中的代码将proje

我是objective C的新手,我正在尝试…尝试在tableviewcontroller代码中设置模型对象的integer属性。以下是模型对象项目的顶部。h:

#import <Foundation/Foundation.h>
@interface Project : NSObject
@property (nonatomic) NSInteger *selecto;
@end
我已使用app委托的didFinishLaunchingWithOptions方法中的代码将project实例添加到数组中,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
projects = [NSMutableArray arrayWithCapacity:20];
Project *project = [[Project alloc] init];
project.selecto = 0;
[projects addObject:project];
return YES;
}
TableViewController的didselectmethod是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int selectedRow = [indexPath row];
Project *pro = [projects objectAtIndex:selectedRow] ;
*pro.selecto = 1;//errors here with exc_bad_access code=2 
}
每当我运行它时,它在指定属性selecto时冻结,我得到一个n错误: exc\u错误\u访问代码=2
可能是一个简单的新手问题-但我花了大约8个小时试图解决这个问题…也许我需要一个不同的爱好…

问题是您的属性
selecto
是一个指向长整数的指针,在
中,您使用选项完成启动:
将其设置为0,即
nil
。在
didSelectRowAtIndexPath:
中,无论selecto指向什么,您都试图将其设置为1。

只需使用
@property(非原子)NSInteger selecto
pro.selecto=1取而代之。

谢谢你做到了……一直就在我面前。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int selectedRow = [indexPath row];
Project *pro = [projects objectAtIndex:selectedRow] ;
*pro.selecto = 1;//errors here with exc_bad_access code=2 
}