传递对象时出现问题

发布于 2024-12-05 15:35:19 字数 1317 浏览 0 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪之魂 2024-12-12 15:35:19

考虑到最后一段代码中的 CCV 是 ivar,请尝试这样做:

CCV = [ccv retain];

您不需要为现有对象分配空间。另外,请小心您的 init 方法模式,您可能需要查看 文档

Considering CCV in your last snippet of code is an ivar, try this instead:

CCV = [ccv retain];

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.

乄_柒ぐ汐 2024-12-12 15:35:19

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 are nil.

Check NSUserDefaults Class Reference

痴情换悲伤 2024-12-12 15:35:19

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.

二智少女 2024-12-12 15:35:19

这是有效的结果代码:

- (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 keep] 需要存在

here is the resulting code that works:

- (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;
}

and the sender method:

- (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;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文