私はプレーヤーが車を使用するiPhoneアプリケーションを作っています。私はプレーヤーが使用したい車色の設定を保存できるようにしたい。私がプレーヤーの車の色を保存するために作ったファイルを読むと、グローバルNSString変数は値を変更しません。ここに私のコードです:変数carColorが正しいことを割り当てられているnsstringの割り当てに関する問題
NSString *carColor;
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"carColor" ofType:@"txt"];
if(filePath2){
carColor = [NSString stringWithContentsOfFile:filePath2 encoding:NSUTF8StringEncoding error:NULL];
}
currentCarImage = [NSString stringWithString:carColor];
が、私は、デバッガで見たとき、それはcurrentCarImageが空の文字列であることを述べています。もし私がこれを置くなら:
currentCarImage = @"greenCar.png";
それはうまくいくでしょう。私もこれを試した:
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"carColor" ofType:@"txt"];
if(filePath2){
currentCarImage = [NSString stringWithString:[NSString stringWithContentsOfFile:filePath2 encoding:NSUTF8StringEncoding error:NULL]];
}
しかし、これも機能しませんでした。何か案は?
編集: 私はそれをある方法で動作させることができましたが、まだそれほど満足できるものではありません。コードを次のように変更しました:
NSString *carColor;
NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"carColor" ofType:@"txt"];
if(filePath2){
carColor = [NSString stringWithContentsOfFile:filePath2 encoding:NSUTF8StringEncoding error:NULL];
}
if([carColor isEqualToString:@"greenCar.png"]){
currentCarImage = @"greenCar.png";
}
if([carColor isEqualToString:@"blueCar.png"]){
currentCarImage = @"blueCar.png";
}
if([carColor isEqualToString:@"redCar.png"]){
currentCarImage = @"redCar.png";
}
if([carColor isEqualToString:@"purpleCar.png"]){
currentCarImage = @"pupleCar.png";
}
if([carColor isEqualToString:@"yellowCar.png"]){
currentCarImage = @"yellowCar.png";
}
if([carColor isEqualToString:@"turquoiseCar.png"]){
currentCarImage = @"turquoiseCar.png";
}
は、stingWithString' 'のない点がないだけ' currentCarImage = [NSStringのstringWithContentsOfFile:filePath2エンコーディング:NSUTF8StringEncodingエラー:NULL]; '十分なものです。提案:コードを書いている理由を正確に知る時間を取る。 – zaph
でも、残念ながらそれはうまくいきませんでした。 –
値は空の文字列または '(null)'として表示されますか? –