[問題] 讀取NSMutableArray特定位置的值、修正輸出順序

作者: Talenius (Harmonic Inspirations)   2016-02-28 11:23:24
已先使用FMDB讀取sqlite的某一table J的值
程式:
NSMutableArray* JData = [NSMutableArray arrayWithCapacity: 0];
FMResultSet *rs = [db executeQuery: @"SELECT uid, uidY, uidH, uidT, uidO from J"];
while ([rs next]) {
int uid = [rs intForColumn: @"uid"];
int uidY = [rs intForColumn: @"uidY"];
int uidH = [rs intForColumn: @"uidH"];
int uidT = [rs intForColumn: @"uidT"];
int uidO = [rs intForColumn: @"uidO"];
[JData addObject: [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: uid], @"uid",
[NSNumber numberWithInt: uidY], @"uidY",
[NSNumber numberWithInt: uidH], @"uidH",
[NSNumber numberWithInt: uidT], @"uidT",
[NSNumber numberWithInt: uidO], @"uidO",
nil]];
}
NSLog(@"%@", JData[2]);
結果:
uid = 3;
uidH = 0;
uidO = 3;
uidT = 0;
uidY = 98;
程式設計的目的:
讀取table J的值轉成二維陣列
以便直接讀取特定位置的值進行運算
請問:
1. 輸出結果順序與讀取順序不同,應如何修正?
讀取sqlite的table J欄位順序:uid, uidY, uidH, uidT, uidO
但顯示讀取後的欄位值順序卻是:uid, uidH, uidO, uidT, uidY
2. 如何讀取特定位置的值?
資料庫就像二維陣列
雖然可以讀取某列的值(如:JData[2]列出uid, uidH, uidO, uidT, uidY)
但要如何讀取某列某行的值(如:JData[2][2])
謝謝
作者: appleway (蘋果愛天空)   2016-02-28 11:28:00
JData[2][@"uid"], JData[2][@"uidH"], etc
作者: tentenlee (天天)   2016-02-28 13:25:00
nsdictionary的key沒有順序的概念 只能在使用的時候去排順序 你要用JData[2][2]的話裡面的資料要是array不然依照目前結構的話就用一樓的那個方式就可以了
作者: Talenius (Harmonic Inspirations)   2016-02-28 23:20:00
感謝指點

Links booklink

Contact Us: admin [ a t ] ucptt.com