NSString* anotherString = [NSString stringWithFormat:@"%d %s", 1, @"String"]
时间: 2024-04-25 15:24:59 浏览: 188
这是一个使用`stringWithFormat:`方法创建一个新的`NSString`对象的示例代码。它使用格式化字符串来将整数和字符串合并到一个新的字符串中。具体来说,它将整数1和字符串"String"插入到格式化字符串中的占位符处。
请注意,这段代码使用的是Objective-C语言,用于iOS和Mac开发。在这里,`NSString`是一个表示字符串的类,`%d`是整数的格式说明符,`%s`是C风格字符串的格式说明符。
相关问题
//urlEncode编码 + (NSString *)urlEncodeStr:(NSString *)input { NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "; NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet]; NSString *upSign = [input stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters]; return upSign; } //urlEncode解码 + (NSString *)decoderUrlEncodeStr: (NSString *) input { NSMutableString *outputStr = [NSMutableString stringWithString:input]; [outputStr replaceOccurrencesOfString:@"+" withString:@"" options:NSLiteralSearch range:NSMakeRange(0,[outputStr length])]; return [outputStr stringByRemovingPercentEncoding]; } 转成python代码
Python中有自带的urllib库可以进行URL编码和解码,代码如下:
```python
import urllib.parse
def urlEncodeStr(input):
charactersToEscape = "!?@#$^&%*+,:;='\"`<>()[]{}/\\| "
allowedCharacters = urllib.parse.quote(charactersToEscape, safe='')
upSign = urllib.parse.quote(input, safe=allowedCharacters)
return upSign
def decoderUrlEncodeStr(input):
outputStr = urllib.parse.unquote(input.replace("+", ""))
return outputStr
```
将这段代码改为按照收缩压和舒张压的分级范围查询,要有是否需要同时满足舒张压和收缩压的选项,不再按照value值查询 - (NSMutableArray *)getBloodPresureDataField:(NSString *)field value:(NSInteger)value startTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime { __block NSMutableArray *array = [NSMutableArray array]; [[HWFitnessMgr sharedInstance] excuteInTransaction:^(FMDatabase *db ,BOOL * rollBack) { NSMutableString *rs = [NSMutableString string]; [rs appendString:@" where"]; //此处逻辑:若搜索字段不为COLUMN_SYNC_CLOUD_STATUS去掉本地已删除未上云的数据 if ([field isEqualToString:COLUMN_SYNC_CLOUD_STATUS]) { [rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ = %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, field, (long)value]; } else { [rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ = %ld and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, field, (long)value, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync]; } [rs appendFormat:@" order by %@ desc", Column_TimeStamp]; NSString *strSql = [@"select * from '%@'" stringByAppendingString:rs]; NSString *bloodPressureTable = [self getTableName:FitnessSamplePointBloodPressureTable]; FMResultSet * detailRs = [[HWFitnessMgr sharedInstance] selectBy:strSql andTable:bloodPressureTable inDatabase:db]; if (detailRs) { while ([detailRs next]) { [array addObject:[self fetchBloodPressureDataWithDetailRs:detailRs]]; } [detailRs close]; } }]; return array; }
以下是修改后的代码:
- (NSMutableArray *)getBloodPresureDataField:(NSString *)field systolicValue:(NSInteger)systolicValue diastolicValue:(NSInteger)diastolicValue startTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime requireBoth:(BOOL)requireBoth {
__block NSMutableArray *array = [NSMutableArray array];
[[HWFitnessMgr sharedInstance] excuteInTransaction:^(FMDatabase *db ,BOOL * rollBack) {
NSMutableString *rs = [NSMutableString string];
[rs appendString:@" where"];
//此处逻辑:若搜索字段不为COLUMN_SYNC_CLOUD_STATUS去掉本地已删除未上云的数据
if ([field isEqualToString:COLUMN_SYNC_CLOUD_STATUS]) {
if (requireBoth) {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ >= %ld and %@ <= %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYSTOLIC, (long)systolicValue, COLUMN_DIASTOLIC, (long)diastolicValue];
} else {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and ((%@ >= %ld and %@ <= %ld) or (%@ >= %ld and %@ <= %ld))", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYSTOLIC, (long)systolicValue, COLUMN_SYSTOLIC, (long)diastolicValue, COLUMN_DIASTOLIC, (long)systolicValue, COLUMN_DIASTOLIC, (long)diastolicValue];
}
} else {
if (requireBoth) {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ >= %ld and %@ <= %ld and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, field, (long)systolicValue, field, (long)diastolicValue, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync];
} else {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and ((%@ >= %ld and %@ <= %ld) or (%@ >= %ld and %@ <= %ld)) and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYSTOLIC, (long)systolicValue, COLUMN_SYSTOLIC, (long)diastolicValue, COLUMN_DIASTOLIC, (long)systolicValue, COLUMN_DIASTOLIC, (long)diastolicValue, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync];
}
}
[rs appendFormat:@" order by %@ desc", Column_TimeStamp];
NSString *strSql = [@"select * from '%@'" stringByAppendingString:rs];
NSString *bloodPressureTable = [self getTableName:FitnessSamplePointBloodPressureTable];
FMResultSet * detailRs = [[HWFitnessMgr sharedInstance] selectBy:strSql andTable:bloodPressureTable inDatabase:db];
if (detailRs) {
while ([detailRs next]) {
[array addObject:[self fetchBloodPressureDataWithDetailRs:detailRs]];
}
[detailRs close];
}
}];
return array;
}
修改后的代码根据收缩压和舒张压的分级范围查询数据,可选是否需要同时满足收缩压和舒张压。当 requireBoth 为 YES 时,只返回同时满足收缩压和舒张压的数据;当 requireBoth 为 NO 时,返回满足收缩压或舒张压的数据。同时,修改了变量名和注释,增强了代码的可读性。
阅读全文