我从SQLite获取数据并希望只获取XCode中列id的最后一个值。代码是
NSString *selquery = @"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;
valvar = [_uid lastObject];
NSNumber *custval = [_uid valueForKey: @"@lastObject"];
NSString *imgval1 = [NSString stringWithFormat:@"%@_%s",custval,"1"];
NSLog(@"%@", imgval1);
请告诉我如何才能获得该值,因为通过使用上面的代码我得到的数组的最后一个id值。
答案 0 :(得分:0)
我不完全理解你的代码结构嘿嘿。试试这个:
NSString *selquery = @"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];
*
NSString *str = [NSString stringWithFormat@"%@",custval];
str = [str stringByReplacingOccurrencesOfString:@"("
withString:@""];
NSString *finalCustval = [NSString stringWithFormat@"%@",str];
finalCustval = [finalCustval stringByReplacingOccurrencesOfString:@")"
withString:@""];
*
NSString *imgval1 = [NSString stringWithFormat:@"%@_%s",finalCustval ,"1"];
NSLog(@"%@", imgval1);
<强>更新强>
尝试添加带*的那些。
答案 1 :(得分:0)
我认为你的情况就是这样...... 可能帮助你
NSArray *temp=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
// NSString *tmmp=[temp0ne lastObject];
NSArray *finalStr=[uid lastObject];
NSLog(@"dictonarry is---->%@",[finalStr lastObject]);
<强>输出强>
<强> 3_1 强>
修改强>
NSArray *temp=[NSArray arrayWithObjects:@"(1)",@"(2)",@"(3)", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
NSString *tmmp=[temp0ne lastObject];
NSString *final=[tmmp stringByReplacingOccurrencesOfString:@"(" withString:@""];
final=[final stringByReplacingOccurrencesOfString:@")" withString:@""];
NSString *imgval1 = [NSString stringWithFormat:@"%@_%s",final,"1"];
NSLog(@"%@", imgval1);
我不知道这是正确的方式或不尝试这个....否则看看这个link