lsdoy
lsdoy

Reputation: 129

Make a macro: how to embed string correctly

#define SDNSPredicate(key,value) \
    [NSPredicate predicateWithFormat:@"#key == %@",value];

When I use SDNSPredicate(@"hCName",@"ccc"), I expect hCName == "ccc" but it becomes key == "ccc"

How to make it right?

Upvotes: 1

Views: 87

Answers (2)

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81868

how to make it right?

Use a function. Macros are evil.

static inline NSPredicate *SDNSPredicate(NSString *key, NSString *value) {
    return [NSPredicate predicateWithFormat:@"%@ == %@", key, value];
}

Upvotes: 5

Govind Prajapati
Govind Prajapati

Reputation: 957

Solution:

#define SDNSPredicate(key,value) \
[NSPredicate predicateWithFormat:@"%K == %@",key,value];

Upvotes: 2

Related Questions