使用框架时如何将 NSMutableString 转换为 NSString?

发布于 2024-09-02 04:08:24 字数 1128 浏览 10 评论 0原文

我编写了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人爱我别走 2024-09-09 04:08:24

我猜你忘记分配/初始化你的字符串......

I'm guessing that you're forgetting to alloc/init your strings...

谁把谁当真 2024-09-09 04:08:24

确保 outputBuilder 有效。你在哪里alloc/init'ing它?

Make sure outputBuilder is valid. Where are you alloc/init'ing it?

雪若未夕 2024-09-09 04:08:24

您的 doThis: 方法似乎没有初始化 outputBuilder。因此,如果它是空指针,则不会对其执行任何操作。

Your doThis: method doesn't seem to initialise outputBuilder. So if it is a null pointer, then nothing will be done to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文