2016-09-25 1 views
0

'Object_Info'という値を持つキーを含む辞書を作成しようとしています。 私は、次のコードを持っていると、このエラーが発生します。私は変更可能な辞書を作成する必要がありますが、 "互換性のないポインタ型の初期化"エラーが発生しました

#import <Foundation/Foundation.h> 

@interface Object_Info : NSObject 
{ 
    NSString *product; 
    float cost; 
    int qty; 
} 
@end 


@implementation Object_Info 

-(id) init { 
    if (self = [super init]){ 
     product = @""; 
     cost = 0.0; 
     qty = 0;  
    } 
    return self; 
} 
@end 

int main(int argc, const char * argv[]) { 
    @autoreleasepool { 
      NSMutableDictionary *stock = @{ //ERROR IS HERE!!!!! 
           @"Lawn Chair" : [Object_Info new], 
           @"Beach Chair" : [Object_Info new], 
           @"Kitchen Chair" : [Object_Info new], 
           @"Futon" : [Object_Info new], 
           }; 

     for(NSString *key in [stock allKeys]) { 
      NSLog(@"%@",[stock objectForKey:key]); 
     } 

    } 
    return 0; 
} 

答えて

1

あなたが抱えている問題は、あなたがNSMutableDictionaryにNSDictionaryのを割り当てるしようとしているということです。

Incompatible pointer types initializing 'NSMutableDictionary *' with an expression of type 'NSDictionary *' 

はここに私のコードです。

これを試してみてください:

NSMutableDictionary *stock = [NSMutableDictionary new]; 
[stock setDictionary:@{ 
               @"Lawn Chair" : [Object_Info new], 
               @"Beach Chair" : [Object_Info new], 
               @"Kitchen Chair" : [Object_Info new], 
               @"Futon" : [Object_Info new], 
               }]; 
+0

これはあなたの問題を解決した場合は、正しい答えとしてそれをマークすることができますしてください – HarmVanRisk

関連する問題