奇怪的 objective-c 语法 - 方括号和 @ 符号



我在我的一个项目中使用 GHSidebarNav,我遇到了分配对象数组的代码。我只是不知道它在做什么。它只是一个数组吗?这种奇怪的@[...]语法是什么?我以前没有见过:

NSArray *controllers = @[
    @[
        [[UINavigationController alloc] initWithRootViewController:[[GHRootViewController alloc] initWithTitle:@"Profile" withRevealBlock:revealBlock]]
    ],
    @[
        [[UINavigationController alloc] initWithRootViewController:[[GHRootViewController alloc] initWithTitle:@"News Feed" withRevealBlock:revealBlock]],
        [[UINavigationController alloc] initWithRootViewController:[[GHMessagesViewController alloc] initWithTitle:@"Messages" withRevealBlock:revealBlock]],
        [[UINavigationController alloc] initWithRootViewController:[[GHRootViewController alloc] initWithTitle:@"Nearby" withRevealBlock:revealBlock]],
        [[UINavigationController alloc] initWithRootViewController:[[GHRootViewController alloc] initWithTitle:@"Events" withRevealBlock:revealBlock]],
        [[UINavigationController alloc] initWithRootViewController:[[GHRootViewController alloc] initWithTitle:@"Friends" withRevealBlock:revealBlock]]
    ]
];

这些是数组文字,一种容器文字,在 Xcode 4.4 及更高版本中可用。

看:

  • "文字语法"部分,讨论使用目标 C 编程中的数组
  • LLVM 站点上的 Objective-C 文字讨论
  • WWDC 2012 Modern Objective-C,大约19-20分钟。
  • WWDC 2012 迁移到现代 Objective-C

这是一个声明多维数组的新目标 C 文字。

它正在取代[NSArray arrayWithObjects:[NSArray arrayWithObjects:...], [NSArray arrayWithObjects:..]]];

最新更新