使用自动布局可视化格式与斯威夫特? [英] Using Autolayout Visual Format with Swift?

查看:166
本文介绍了使用自动布局可视化格式与斯威夫特?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图使用Autolayout视觉格式语言斯威夫特,使用 NSLayoutConstraint.constraintsWithVisualFormat 。下面是一些code是没有任何用处的例子,但据我可以告诉应使类型检查幸福的:

I've been trying to use the Autolayout Visual Format Language in Swift, using NSLayoutConstraint.constraintsWithVisualFormat. Here's an example of some code that does nothing useful, but as far as I can tell should make the type checker happy:

let foo:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(
  format: "", options: 0, metrics: {}, views: {})

不过,这会触发编译器错误:

However, this triggers the compiler error:

不能转换前pression的类型'[AnyObject]!键入字符串!'。

"Cannot convert the expression's type '[AnyObject]!' to type 'String!'".

在我想这是一个雷达值得的bug,有什么明显的,我在这里丢失?出现这种情况,即使没有变量名的显式转换,或为使用其他无偿的向下转换。我看不出有任何理由,编译器会料到这个任何部分解析为字符串!

Before I assume this is a Radar-worthy bug, is there anything obvious I'm missing here? This happens even without the explicit casting of the variable name, or with other gratuitous downcasting using as. I can't see any reason why the compiler would be expecting any part of this to resolve to a String!.

推荐答案

这对我的作品,并没有错误:

this works for me with no error:

let bar:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(
  nil, options: NSLayoutFormatOptions(0), metrics: nil, views: nil)

更新

线之上,不得编译自1 ST 4 参数不能的自选的了。

update

the line above may not be compiled since the 1st and 4th parameters cannot be optionals anymore.

语法的那些已被设置,例如像这样的:

syntactically those have to be set, like e.g. this:

let bar:[AnyObject] = NSLayoutConstraint.constraintsWithVisualFormat("", options: NSLayoutFormatOptions(0), metrics: nil, views: ["": self.view])


更新

(对于X code 7,雨燕2.0​​)

有效的语法现在请求参数的名称,以及,如:

the valid syntax now requests the parameters's name as well, like:

NSLayoutFormatOptions(rawValue: 0)


注:此行code的显示正确的语法只,参数本身并不能保证约束将是正确的,甚至有效

这篇关于使用自动布局可视化格式与斯威夫特?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆