stringWithFormat:数据类型未知
我正在制作一个框架,并且有以下代码(conditions
是 NSDictionary):
for (NSString *key in conditions) {
id value = [[conditions valueForKey:key] retain];
// Check if number or string
if ([value class] == [NSString class]) {
conditionsString = [NSString stringWithFormat:@"%@='%@' ", key, value];
} else {
conditionsString = [NSString stringWithFormat:@"%@=%@ ", key, value];
}
}
如果 value
是 NSString,则应添加引号,否则不应添加代码。但是,如果不是 NSString,我不知道 value
的数据类型,因为它可能是 NSNumber、NSInteger、int、float、double 等...我不能只使用 %@
或 %d
但我确信有人知道如何做到这一点?谢谢。
I am making a framework and I have this code (conditions
is an NSDictionary):
for (NSString *key in conditions) {
id value = [[conditions valueForKey:key] retain];
// Check if number or string
if ([value class] == [NSString class]) {
conditionsString = [NSString stringWithFormat:@"%@='%@' ", key, value];
} else {
conditionsString = [NSString stringWithFormat:@"%@=%@ ", key, value];
}
}
If the value
is an NSString, quotes should be added, otherwise codes should not be added. However, if not an NSString, I don't know the datatype of value
, as it could be NSNumber, NSInteger, int, float, double, etc... I can't just use %@
or %d
but I'm sure someone on SO knows how to do this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个。
编辑:在你的情况下这很简单,因为 nsdictionary 只能包含 nsobjects。所以 %@ 就可以了。
have a look at this.
edit: in your case it's pretty easy, since nsdictionary can only contain nsobjects. so %@ would do the trick.
您可以检查类(kindOfClass),然后使用适当的格式化程序。
例如
You could check the class (kindOfClass) and then use appropriate formatter.
e.g.