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.

2
  • 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. Commented Oct 2, 2010 at 23:55
  • Aha, so all numbers should be an NSNumber? Commented Oct 3, 2010 at 12:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

e.g.

if ( [myObj isKindOfClass:[NSString class]] )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.