我知道c通过引用传递并确定我的问题所在,但对于我的生活,我无法弄明白这一点。 (也可能有一个框架或更合适的方式来做这个,我可以接受建议)
CrestronControllerValues只是一个getter和setter类
我在我的app委托中初始化并传递它:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *keys = [NSArray arrayWithObjects:@"IPaddress", @"PortNumber", nil];
NSArray *objs = [NSArray arrayWithObjects:@"10.8.30.111", @"41794", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
[defaults registerDefaults:dict];
CrestronControllerValues *CCV = [[[CrestronControllerValues alloc]init]autorelease];
[CCV setIPID:3];
[CCV setIPaddress:[defaults stringForKey:@"IPaddress"]];
[CCV setPortNumber:[defaults stringForKey:@"PortNumber"]];
cClient = [[CrestronClient alloc] initWithCCV:CCV];
你可以看到最后一行将它传递给另一个类 这是我的问题发挥作用的地方 如果我尝试使用getipaddress或getportnumber我会得到错误的访问
- (id)initWithCCV:(CrestronControllerValues *)ccv
{
[super init];
CCV = [CrestronControllerValues alloc];
CCV = ccv;
port = [[ccv getPortNumber] intValue];
ip = [ccv getIPaddress];
NSLog(@"ip %@ ~ port %@", ip, port);
return self;
}
我尝试过多种方式,包括 cClient.ccv = ccv(与使用init发送它相反) 尝试为自己添加一个getter,以便它是cClient = [[CrestronClient alloc] initWithCCV:[CCV getSelf]];
答案 0 :(得分:2)
考虑到最后一段代码中的CCV
是一个ivar,请尝试这样做:
CCV = [ccv retain];
您不需要为现有对象分配空间。另外,请注意您的init方法模式,您可能需要查看documentation。
答案 1 :(得分:2)
在NSUserdefaults
中存储对象的正确方法是[defaults synchronize]
。您的访问权限很差,因为您从默认设置获得的对象是nil
。
答案 2 :(得分:0)
是否合成了IPaddress和PortNumber?如果你想从另一个类访问这些变量,你需要确保在CrestronControllerValues.h中为每个对象和你的CrestronControllerValues.m声明@property
,除非你@synthesize IPaddress, PortNumber
否则明确地声明你的getter和setter方法。
答案 3 :(得分:0)
这是生成的代码:
- (id)initWithCCV:(CrestronControllerValues *)ccv
{
self = [super init];
if (self)
{
socket = [[LXSocket alloc] init];
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
sqliteLogger = [[FMDBLogger alloc] initWithLogDirectory:[self applicationFilesDirectory]];
sqliteLogger.saveThreshold = 500;
sqliteLogger.saveInterval = 60; // 60 seconds
sqliteLogger.maxAge = 60 * 60 * 24 * 7; // 7 days
sqliteLogger.deleteInterval = 60 * 5; // 5 minutes
sqliteLogger.deleteOnEverySave = NO;
[DDLog addLogger:sqliteLogger];
// CCV = [CrestronControllerValues alloc];
CCV = [ccv retain];
port = [CCV getPortNumber];
ip = [CCV getIPaddress];
NSLog(@"ip %@ ~ port %@", ip, port );
}
return self;
}
和发送方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
[ConnectCommand addTarget:self action:@selector(command1ButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[SendJoin addTarget:self action:@selector(command3ButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[UpdateLog addTarget:self action:@selector(updateLogButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *keys = [NSArray arrayWithObjects:@"IPaddress", @"PortNumber", nil];
NSArray *objs = [NSArray arrayWithObjects:@"10.8.30.111", @"41794", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
[defaults registerDefaults:dict];
CrestronControllerValues *CCV = [[CrestronControllerValues alloc]init];
[CCV setIPID:3];
[CCV setIPaddress:[defaults stringForKey:@"IPaddress"]];
[CCV setPortNumber:[defaults stringForKey:@"PortNumber"]];
// NSLog(@"ip from defaults %@ ~ ip from ccv %@",[defaults stringForKey:@"IPaddress"], [CCV getIPaddress] );
cClient = [[CrestronClient alloc] initWithCCV:CCV];
[CCV release];
return YES;
}
问题的另一部分是端口是一个int 并试图将一个nsstring传递给它导致崩溃 但[ccv retain]需要在那里