-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 を逆参照する場合)。

これは、さらに微調整しないとうまくいきません。顧客リリース/保持ハンドラーをインストールすることにより、非 NSObject を CFDictionary に保持させることができます。

于 2011-06-08T05:42:57.573 に答える