2013-10-09 43 views
0

我想自动生成的字符串这样cover.image_size(300x400)
动态密钥
IMAGE_SIZE静态字符串
动态数
动态数
如何用#define生成字符串?

我想用#define

#define WADImageSize(Key,Height,Width) (Key @".image_size" @"("Height @"x" [email protected]")") 

NSLog(@" %@",WADImageSize(@"cover", @"300", @"400")); 
Result will be like this **cover.image_size(300x400)** 

这项工作很好,但我想用变量 “封面”, “高度” 和 “宽度”

我尝试财产以后liek这个//#定义DLogW(FMT,... )(FMT ## VA_ARGS),但没有工作..

NSLog(@" %@",WADImageSize(key, height, width)); 

任何帮助吗?


+0

所以这个宏的输出是一个常量'NSString'? – trojanfoe

+2

为什么不使用WADImageSize函数而不是宏 –

+0

我猜他不会工作,因为它必须做的事情太多。 – Shebuka

回答

1

尝试此(从水晶球代码):

#define WADImageSize(Key, Height, Width) ([NSString stringWithFormat:@"%@.image_size(%dx%d)", Key, Height, Width]) 
1

定义大多用于常量。可能像这样的方法会更好。

-(NSString*)WADImageSizeWithKey:(NSString*)Key Height:(NSUInteger)Height Width:(NSUInteger)Width 
{ 
    return [NSString stringWithFormat:@"%@.image_size(%ux%u)", Key, Height, Width]; 
} 

还记得你不能调试定义。

+0

'NSUInteger'和'%d'? – trojanfoe