使用框架时如何将 NSMutableString 转换为 NSString?
我编写了一个 Objective-C 框架,它使用 NSMutableString 构建一些 HTML 代码,该代码将值作为 NSString 返回。
我已经在接口 .h 文件中声明了 NSString 和 NSMutableString:
NSString *_outputLanguage; // Tests language output
NSMutableString *outputBuilder;
NSString *output;
这是框架实现 .m 代码中的示例(我无法打印实际代码,因为它是专有的):
-(NSString*)doThis:(NSString*)aString num:(int)aNumber {
if ([outputBuilder length] != 0) {
[outputBuilder setString:@""];
}
if ([_outputLanguage isEqualToString:@"html"]) {
[outputBuilder appendString:@"Some Text..."];
[outputBuilder appendString:aString];
[outputBuilder appendString:[NSString stringWithFormat:@"%d", aNumber]];
}
else if ([_outputLanguage isEqualToString:@"xml"]) {
[outputBuilder appendString:@"Etc..."];
}
else {
[outputBuilder appendString:@""];
}
output = outputBuilder;
return output;
}
当我编写文本程序时,NSLog 只是打印出“ (无效的)”。我在那里写的代码是:
TheClass *instance = [[TheClass alloc] init];
NSString *testString = [instance doThis:@"This String" num:20];
NSLog(@"%@", testString);
[instance release];
我希望这是足够的信息!
I have written an Objective-C framework which builds some HTML code with NSMutableString which returns the value as an NSString.
I have declared an NSString and NSMutableString in the inteface .h file:
NSString *_outputLanguage; // Tests language output
NSMutableString *outputBuilder;
NSString *output;
This is a sample from the framework implementation .m code (I cannot print the actual code as it is proprietary):
-(NSString*)doThis:(NSString*)aString num:(int)aNumber {
if ([outputBuilder length] != 0) {
[outputBuilder setString:@""];
}
if ([_outputLanguage isEqualToString:@"html"]) {
[outputBuilder appendString:@"Some Text..."];
[outputBuilder appendString:aString];
[outputBuilder appendString:[NSString stringWithFormat:@"%d", aNumber]];
}
else if ([_outputLanguage isEqualToString:@"xml"]) {
[outputBuilder appendString:@"Etc..."];
}
else {
[outputBuilder appendString:@""];
}
output = outputBuilder;
return output;
}
When I wrote a text program, NSLog simply printed out "(null)". The code I wrote there was:
TheClass *instance = [[TheClass alloc] init];
NSString *testString = [instance doThis:@"This String" num:20];
NSLog(@"%@", testString);
[instance release];
I hope this is enough information!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你忘记
分配/初始化
你的字符串......I'm guessing that you're forgetting to
alloc/init
your strings...确保
outputBuilder
有效。你在哪里alloc/init
'ing它?Make sure
outputBuilder
is valid. Where are youalloc/init
'ing it?您的
doThis:
方法似乎没有初始化outputBuilder
。因此,如果它是空指针,则不会对其执行任何操作。Your
doThis:
method doesn't seem to initialiseoutputBuilder
. So if it is a null pointer, then nothing will be done to it.