字符串作为映射中的键

发布于 2024-11-14 02:48:06 字数 1803 浏览 8 评论 0原文

我想使用字符串键将对象存储在 NSMutableDictionary 中,因此我进行了如下声明:

[m_pMsgIdToStructMap setObject:pStruct 
                        forKey:[NSMutableString stringWithString:pStruct->szAsciiName]];

szAsciiName 被声明为 NSMutableString *在 .h 文件中。 我没有收到任何警告或错误,但我想确认我所做的声明是否正确。

编辑:

嗨,

main.m
-------

#import <Foundation/Foundation.h>
#import "string.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    stringss* stringsss = [[stringss alloc]init];
    //NSMutableString* strs = [[NSMutableString alloc]initWithFormat:"First"];
    [stringsss trail:@"First"]; //getting warning:Passing argument 1 of 'trail' from incompatible pointer type

    [pool drain];
    return 0;
}

stringss.h
----------

#import <Foundation/Foundation.h>

typedef struct
{
    int a;
    int b;
    NSMutableString* szAsciiName;

}st;
@interface stringss : NSObject {

    NSMutableDictionary* m_map;

}
-(void)trail:(const char*)szAsciiName;

@end

stringss.m
---------

#import "string.h"

@implementation stringss

-(id)init
{
    if (self = [super init]) {
        m_map = [[NSMutableDictionary alloc]init];
        //pStruct->szAsciiName = [[NSMutableString alloc]init];
    }
    return self;
}
-(void)trail:(NSString*)szAsciiName
{
    st* pStruct = malloc(sizeof(st));
    st* pStruct1 = malloc(sizeof(st));

    pStruct->a = 10;
    pStruct->b = 20;
    pStruct->szAsciiName = [[NSMutableString alloc]init];
    pStruct->szAsciiName = (NSMutableString*)szAsciiName;

    [m_map setObject:(void*)pStruct forKey:szAsciiName];

    pStruct1 = (st*)[[m_map objectForKey:szAsciiName]bytes];
}

@end

我也收到 EXC_BAD_ACCESS 异常。

I want to store an object in an NSMutableDictionary with a string key, so I have made the declaration like this:

[m_pMsgIdToStructMap setObject:pStruct 
                        forKey:[NSMutableString stringWithString:pStruct->szAsciiName]];

szAsciiName is declared as NSMutableString * in the .h file.
I am not getting any warning or error, but I want to confirm whether the declaration that I'm making is correct.

EDITED:

Hi,

main.m
-------

#import <Foundation/Foundation.h>
#import "string.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    stringss* stringsss = [[stringss alloc]init];
    //NSMutableString* strs = [[NSMutableString alloc]initWithFormat:"First"];
    [stringsss trail:@"First"]; //getting warning:Passing argument 1 of 'trail' from incompatible pointer type

    [pool drain];
    return 0;
}

stringss.h
----------

#import <Foundation/Foundation.h>

typedef struct
{
    int a;
    int b;
    NSMutableString* szAsciiName;

}st;
@interface stringss : NSObject {

    NSMutableDictionary* m_map;

}
-(void)trail:(const char*)szAsciiName;

@end

stringss.m
---------

#import "string.h"

@implementation stringss

-(id)init
{
    if (self = [super init]) {
        m_map = [[NSMutableDictionary alloc]init];
        //pStruct->szAsciiName = [[NSMutableString alloc]init];
    }
    return self;
}
-(void)trail:(NSString*)szAsciiName
{
    st* pStruct = malloc(sizeof(st));
    st* pStruct1 = malloc(sizeof(st));

    pStruct->a = 10;
    pStruct->b = 20;
    pStruct->szAsciiName = [[NSMutableString alloc]init];
    pStruct->szAsciiName = (NSMutableString*)szAsciiName;

    [m_map setObject:(void*)pStruct forKey:szAsciiName];

    pStruct1 = (st*)[[m_map objectForKey:szAsciiName]bytes];
}

@end

I'm getting EXC_BAD_ACCESS exception also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

最偏执的依靠 2024-11-21 02:48:06

据猜测,pStruct 不是一个 NSObject,它是一个原始结构(假设您使用 -> 来取消引用 szAsciiName)。

如果不进行更多调整,这是行不通的。您可以通过安装客户释放/保留处理程序来使 CFDictionary 保留非 NSObject。

At a guess, pStruct is not an NSObject, its a raw structure (given your use of -> to dereference the szAsciiName).

That isn't going to work without more tweaking. You can make a CFDictionary hold on to non-NSObject's by installing customer release/retain handlers.

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