Objective C initWithNibName

发布于 2024-11-13 07:58:46 字数 470 浏览 3 评论 0原文

我在弄清楚 Apple 教程中的这个函数代表什么时遇到了一些困难:

[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]]

我的问题是:MyViewController 之前的“@”代表什么?

另外,因为我正在关注 this教程,我想知道:我的项目中有笔尖:自动生成的MainWindow.xib和MyViewController.xib - 我想知道:如果我只需要一个,为什么我有两个?

提前致谢!

I am facing some difficulties figuring out what this function in the Apple tutorials stands for:

[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]]

My questions are: what does the "@" stand for before MyViewController?

Addionally, as I am following this tutorial, I was wondering: There are to nibs in my project: the automatically generated MainWindow.xib and MyViewController.xib – I was wondering: why do I have two if I only need one?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦醒灬来后我 2024-11-20 07:58:46

字符串前面的 @ 符号表示该字符串是 NSString 的实例。

@"Hello" <-- NSString object
"Hello"  <-- Null-terminated char array (C-string)

您甚至可以向它发送消息:

[@"Hello" stringByAppendingString:@" World!"]

您将比 C 字符串更频繁地使用 NSString 对象。

如果您想将 C 字符串转换为 NSString 对象(例如,如果您使用返回此类字符串的 C 库),您可以使用以下内容:

char *myCstring = "Hello, World!";
NSString *myString = [NSString stringWithUTF8String:myCstring];

关于两个笔尖:实际上您根本不需要任何笔尖,但是苹果喜欢通过使用只有一个窗口的笔尖来降低性能。我不知道他们为什么这样做,但你可以用一行代码创建一个窗口,这样编译和运行速度会快得多。

在您的 applicationDidFinishLaunching: 方法中:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
// now you can remove the MainWindow.xib nib.

我个人更喜欢完全不使用笔尖,但这是个人的选择。

The @-sign before a string literal means that the string is an instance of NSString.

@"Hello" <-- NSString object
"Hello"  <-- Null-terminated char array (C-string)

You can even send messages to it:

[@"Hello" stringByAppendingString:@" World!"]

You will use NSString objects more often than C-strings.

If you want to convert a C-string to an NSString object (if you are using C libraries that return such strings for example), you could use this:

char *myCstring = "Hello, World!";
NSString *myString = [NSString stringWithUTF8String:myCstring];

About the two nibs: actually you don't need any nibs at all, but Apple likes to decrease performance by using a nib that only has one window. I don't know why they do it, but you can create a window in code with only one line, which compiles and runs much faster.

In your applicationDidFinishLaunching: method:

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil] autorelease];
// now you can remove the MainWindow.xib nib.

Personally I prefer to use no nibs at all, but that's one's own choice.

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