นี่คือรหัส:
NSString* filepath = [[NSBundle mainBundle]
pathForResource:@"Stones" ofType:@"plist"];
NSDictionary *stones = [NSDictionary dictionaryWithContentsOfFile:filepath];
NSArray* array = stones[@"StoneArray"];
NSDictionary* dic = array[stoneInt];
NSString* string = [[NSString alloc] init];
string = dic[[NSString stringWithFormat:@"%d", stoneInt]];
return string;
stoneInt
เป็นตัวเลขตั้งแต่ 0-59 (ในกรณีเริ่มต้นคือ 11)
เมื่อฉันตั้งค่าเบรกพอยต์ที่คำสั่ง return
stoneInt
จะเป็น 11 (ตามที่ควรจะเป็น) แต่ string
ก็คือ nil
ผมทำอะไรผิดหรือเปล่า?
นอกจากนี้ เมื่อฉันวิเคราะห์ dic
มันแสดงให้เห็นอย่างถูกต้องว่ามันทำงานกับคีย์ 11
และสตริง Maze
นี่คือซอร์สโค้ดสำหรับ plist:
<plist version="1.0">
<dict>
<key>StoneArray</key>
<array>
<dict>
<key>1</key>
<string>Ocean</string>
</dict>
<dict>
<key>2</key>
<string>Grass</string>
</dict>
สิ่งนี้ดำเนินต่อไปจนถึงสตริง 59 สตริงที่ฉันพยายามส่งคืน (ในกรณีใดกรณีนี้ สมมติว่า 1) คือ "มหาสมุทร" วัตถุ string
ของฉันคือ nil
เสมอ
stoneInt
สองครั้งในสองวิธีที่แตกต่างกัน ขั้นแรกให้คุณใช้เป็นดัชนีในarray
ซึ่งจะให้พจนานุกรมแก่คุณ จากนั้นคุณแปลงstoneInt
เป็นสตริงและใช้เป็นคีย์ในพจนานุกรม นั่นดูแปลกจริงๆ - person rmaddy   schedule 01.11.2016NSString* string = array[[NSString stringWithFormat:@"%d", stoneInt]];
- person Anthony Shintoluggenprog   schedule 01.11.2016nil
สำหรับสตริง - person Anthony Shintoluggenprog   schedule 01.11.2016