我对此很新 - 我正在尝试将NSString stringWithFormat与一些文本进行比较.它与stringWithString一起工作正常.我无法弄清楚为什么它不能与stringWithFormat一起使用?非常感谢任何人提供的任何帮助!
NSString *theQuestion = [NSString stringWithFormat:@"The same thing"];
if (theQuestion == @"The same thing"){
NSLog(@"stringWithFormat is the same as the given text");
}else{
NSLog(@"stringWithFormat is NOT the same as the given text");
NSLog(@"but theQuestion is:\"%@\"", theQuestion);
}
Run Code Online (Sandbox Code Playgroud) 我是iOS新手,所以请耐心等待.
我想使用stringWithFormat构造一个字符串,但我想只在条件为真时才放入字符串的一部分.我怎样才能实现像 -
myString = [NSString stringWithFormat:@"%@%@",
@"myString1",
//put myString2 only if (someCondition)]
Run Code Online (Sandbox Code Playgroud)
如果我不够清楚,请告诉我.
为了确保NSString initWithFormat:arguments:按预期返回的格式化字符串是正确的,我需要确定是否有与参数相同数量的格式说明符。下面是一个(稍作和高度编辑的)示例:
- (void)thingsForStuff:(CustomStuff)stuff, ...
{
NSString *format;
switch (stuff)
{
case CustomStuffTwo:
format = @"Two things: %@ and %@";
break;
case CustomStuffThree:
format = @"Three things: %@, %@, and %@";
break;
default:
format = @"Just one thing: %@";
break;
}
va_list args;
va_start(args, method);
// Want to check if format has the same number of %@s as there are args, but not sure how
NSString *formattedStuff = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
NSLog(@"Things: %@", formattedStuff);
}
Run Code Online (Sandbox Code Playgroud)
使用这种方法, …
假设我有这样的方法
+ (NSString *)stringWithObject:(id)object format:(NSString *)format
{
NSString *string = [NSString stringWithFormat:format, object];
NSLog(@"%@", string);
return string;
}
Run Code Online (Sandbox Code Playgroud)
该object参数是从来没有nil,但传递到方法的format参数可能要么是
NSString *formatWithPlaceholder = @"object: %@"
Run Code Online (Sandbox Code Playgroud)
要么
NSString *formatWithoutPlaceholder = @"No object";
Run Code Online (Sandbox Code Playgroud)
所以如果formatWithoutPlaceholder作为格式参数传递给方法,控制台输出是正确的,没有警告或错误,但这会导致其他问题吗?我觉得这样的用法有点不对劲stringWithFormat:.
我是Objective C和iPhone SDK的新手,我正在尝试将以下内容作为在标签区域中显示数字结果的简单示例:
label.text = [NSString stringWithFormat: @"%d", 55];
Run Code Online (Sandbox Code Playgroud)
上面的代码在标签区域显示数字"55".但是,以下代码导致显示"0"(在header文件中将calculateResult声明为双变量类型):
calculationResult = 55;
label.text = [NSString stringWithFormat: @"%d", calculationResult];
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢.
请帮助新手进行iPhone开发.在我的应用程序中,我做了很多,仪器将其显示为泄漏.这样做的正确方法是什么?
我试图将数值数据重新格式化为字符串,以便在NSMutableDictionary对象中使用.所以我觉得如果我这样做会很棒:
[myDict setObject:[NSString stringWithFormat:@"%d", section] forKey:@"Category"];
Run Code Online (Sandbox Code Playgroud)
我不想写3行来做...
NSString *cat = [NSString stringWithFormat:@"%d", section];
[myDict setObject:cat forKey:@"Category"];
[cat release];
Run Code Online (Sandbox Code Playgroud)
如果我必须这样做,但这种短暂使用的最佳做法是什么?
我想自定义一个带有格式化字符串输入和(const char*)返回的方法,但问题如下所示......谁能告诉我如何解决它?谢谢.

我目前有一个自定义格式的int值并显示在标签中.int值总是一个5位数字,我在十分之一到百分之一的位置显示一个+符号.如果number = 12345,则displayNumber = 123 + 45.
ViewController.m
int high = number / 100;
int low = number - (high * 100);
NSString *displayNumber = [NSString stringWithFormat:@"%d+%d", high, low];
Run Code Online (Sandbox Code Playgroud)
我的问题是当数字在十分位数时为零.如果number = 12304,displayNumber将显示为123 + 4,我希望它显示为123 + 04.我尝试使用低于10的if语句,但这与我的其余代码并不完全兼容.是否有任何简单的方法可以使低显示两位数,即使它是一位数?提前谢谢您的时间.
我的函数采用字典参数和可变数量的NSString变量。所有这些组合都放入一个[NSString stringWithFormat:]方法中,并作为NSURLRequest. 该方法如下所示:
- (NSURLRequest *)buildPath:(NSString *)stringPath attributes:(NSString *)attribute, ...
{
va_list list;
NSString *eachObject;
NSMutableArray *args = [NSMutableArray array];
[args addObject:attribute];
va_start(list, attribute);
while ((eachObject = va_arg(list, NSString *))) {
[args addObject:eachObject];
}
va_end(list);
NSString *listOfAttributes = [args componentsJoinedByString:@", "];
NSString *pathURL = _requestString[stringPath];
NSString *path = [NSString stringWithFormat:pathURL, listOfAttributes];
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
return request;
}
Run Code Online (Sandbox Code Playgroud)
这是我调用该方法时的样子:
NSURLRequest *request = [_venueService buildPath:@"categories"
attributes:_venueService.clientID, _venueService.clientSecret, _venueService.todaysDate, nil]; …Run Code Online (Sandbox Code Playgroud) objective-c variadic-functions nsstring stringwithformat ios
我在swift项目中有一个'pickerView'对象.我确实理解Objective-c中的代码,但我不确定如何在Swift中实现它.
Objc方法
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component)
{
case 0://Week
return 7;
case 1://Hour
return 24;
default://Minutes
return 60;//or 7;(10 by 10) //or 13;(5 by 5)
}
}
Run Code Online (Sandbox Code Playgroud)
我只是不确定如何为titleForRow函数实现switch语句.所以stringWithFormat:@"%.2ld",(long)row问题就在于此.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component)
{
case 0://Week
return _dayOfWeek[row];
case 1://Hour
return [NSString stringWithFormat:@"%.2ld",(long)row];
default://Minutes
return [NSString stringWithFormat:@"%.2ld",(long)row];//or ,row*10] //or ,row*5]
}
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我.
问候,达克斯
stringwithformat ×10
objective-c ×8
ios ×5
nsstring ×5
if-statement ×2
cstring ×1
format ×1
int ×1
iphone ×1
logic ×1
swift ×1
uipickerview ×1