Ios 是否在NSString中添加空间?

Ios 是否在NSString中添加空间?,ios,objective-c,cocoa-touch,cocoa,Ios,Objective C,Cocoa Touch,Cocoa,基本上,我有一个共享按钮,可以打开一个撰写电子邮件模式视图。我想在NSStringWithFormat中添加一个选项卡空间。让我们称之为“postText”。当我尝试设置这样的空间时: NSString*postText=[nsstringwithformat:@“\tHello,World!%@”,myPointer] 它不插入任何内容(它返回“Hello,World!myPointerValue”),如果我只使用tab,它会弄乱间距,因为我需要使用tab大约10-20次。我做错什么了吗?您可

基本上,我有一个共享按钮,可以打开一个撰写电子邮件模式视图。我想在NSStringWithFormat中添加一个选项卡空间。让我们称之为“postText”。当我尝试设置这样的空间时:

NSString*postText=[nsstringwithformat:@“\tHello,World!%@”,myPointer]


它不插入任何内容(它返回“Hello,World!myPointerValue”),如果我只使用tab,它会弄乱间距,因为我需要使用tab大约10-20次。我做错什么了吗?

您可以使用
stringByPaddingToLength
函数在字符串中的任何位置添加空格。检查以下示例函数是否具有相同的功能

-(NSString*)stringByAddingSpace:(NSString*)stringToAddSpace spaceCount:(NSInteger)spaceCount atIndex:(NSInteger)index{
    NSString *result = [NSString stringWithFormat:@"%@%@",[@" " stringByPaddingToLength:spaceCount withString:@" " startingAtIndex:0],stringToAddSpace];
    return result;
}
您案例中的示例用法:

NSString *spaceAddedText = [self stringByAddingSpace:@"Hello World!" spaceCount:5 atIndex:0]
NSString *postText = [NSString stringWithFormat:@"%@%@",spaceAddedText,myPointer];

旁注-为什么要使用
stringWithFormat:
?只需执行:
NSString*postText=@“\tHello,World!”
@rmaddy:oops抱歉,忘记在字符串中添加指针值。
myPointer
是对对象的引用吗?请澄清问题。“它不插入任何内容”是什么意思?结果字符串应该是一个制表符,然后是“Hello,World!”,一个空格,然后是
myPointer
所等于的值。制表符不仅仅是4个空格吗?只是手动添加它们@“你好,世界!”它在obj-c中仍然有效吗?