-1

我想NSMutableDictionary用一个字符串键将一个对象存储在一个对象中,所以我做了这样的声明:

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

szAsciiNameNSMutableString *在 .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 异常。

4

1 回答 1

2

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

如果没有更多的调整,这是行不通的。您可以通过安装客户发布/保留处理程序使 CFDictionary 保留到非 NSObject。

于 2011-06-08T05:42:57.573 回答