传递对象时出现问题
我知道 c 通过引用传递,我确信这就是我的问题所在,但我一生都无法弄清楚这一点。 (也可能有一个框架或更合适的方法来做到这一点,我愿意接受建议)
CrestronControllerValues 只是一个 getter 和 setter 类,
我初始化并将其传递到我的应用程序委托中:
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 一起发送) 尝试为 self 添加一个吸气剂,使其为 cClient = [[CrestronClient alloc] initWithCCV:[CCV getSelf]];
i know c passes by reference and im sure thats where my problem lies, but for the life of me i cannot figure this out. (also there may be a framework or a more proper way of doing this, which im open to suggestions)
CrestronControllerValues is just a getter and setter class
i intialize and pass it in my app delegate:
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];
as you can see the last line passes it to another class
this is where my problem comes into play
if i try to use getipaddress or getportnumber i get bad access
- (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;
}
i have tried multiple ways, including
cClient.ccv = ccv (as opposed to sending it with the init)
tried adding a getter for self so that it would be cClient = [[CrestronClient alloc] initWithCCV:[CCV getSelf]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑到最后一段代码中的 CCV 是 ivar,请尝试这样做:
您不需要为现有对象分配空间。另外,请小心您的 init 方法模式,您可能需要查看 文档。
Considering
CCV
in your last snippet of code is an ivar, try this instead:You don't need to allocate space for an existing object. Also, be careful with your init method pattern, you may want to take a look in the documentation.
在
NSUserdefaults
中存储对象的正确方法是[defaults一线同步]
。您的访问权限很差,因为您从默认值获取的对象是nil
。检查 NSUserDefaults 类参考
The correct way to store objects in
NSUserdefaults
is[defaults synchronize]
. You're getting bad access because the objects you get from defaults arenil
.Check NSUserDefaults Class Reference
IP地址和端口号是否已合成?如果您想从另一个类访问这些变量,则需要确保在 CrestronControllerValues.h 中为每个对象声明
@property
,并在 CrestronControllerValues.m 中声明@synthesize IPaddress, PortNumber
除非您以其他方式明确声明您的 getter 和 setter 方法。Have IPaddress and PortNumber been synthesized? If you want to access those variables from another class, you'll need to make sure that in CrestronControllerValues.h, you declare the
@property
for each object and in your CrestronControllerValues.m, you@synthesize IPaddress, PortNumber
unless you're otherwise explicitly declaring your getter and setter methods.这是有效的结果代码:
以及发送方方法:
问题的另一部分是端口是 int
并试图将 nsstring 传递给它导致崩溃
但 [ccv keep] 需要存在
here is the resulting code that works:
and the sender method:
another part of the problem was that port was an int
and trying to pass a nsstring to it caused crashing
but that [ccv retain] needs to be there