2

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.

  • you cannot add a `NSInteger`, `int`, `float`, or `double` to a NSDictionary because they are all C structs. You can only have Objects of type `NSObject` as @nesium has said. – Richard J. Ross III Oct 02 '10 at 23:55
  • Aha, so all numbers should be an NSNumber? –  Oct 03 '10 at 12:28

2 Answers2

4

have a look at this.

edit: in your case it's pretty easy, since nsdictionary can only contain nsobjects. so %@ would do the trick.

nesium
  • 160
  • 1
  • 3
0

You could check the class (kindOfClass) and then use appropriate formatter.

e.g.

if ( [myObj isKindOfClass:[NSString class]] )
AndersK
  • 35,813
  • 6
  • 60
  • 86