Obj C - إنشاء مفاتيح قائمة خصائص فريدة باستخدام "For"
-
09-12-2019 - |
سؤال
for (int x = 0; x < openSlots; x++) {
NSNumber* slotValueSave = [[NSNumber alloc] initWithInt:[[slotValues objectAtIndex:x] intValue]];
NSLog(@"Saving slot %@.", (x+1));
[valueSavesDictionary setObject:slotValueSave forKey:@"Slot Value %i", (x+1)];
}
مرحبًا، أحاول حفظ ما يصل إلى 9 سلاسل والأرقام المقابلة لمفتاح قائمة الخصائص.لا أريد أن أكتب كل مفتاح @"Slot Value 1"، @"Slot Value 2"، وما إلى ذلك.
لذلك كنت أتساءل كيف يمكنني جعل عبارة "for" تُدرج العدد الصحيح "x" بعد كل عنوان ليتم حفظه تحت هذا المفتاح.
شكرًا لك.
المحلول
يستخدم +stringWithFormat:
:
[valueSavesDictionary setObject:slotValueSave forKey:[NSString stringWithFormat:@"Slot Value %i", (x+1)]];
لا تنتمي إلى StackOverflow