字符串作为映射中的键
我想使用字符串键将对象存储在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据猜测,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.