我想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 异常。