小编Luc*_*ugh的帖子

Swift nil有一个数值吗?

这是Swift中的有效代码:

println(nil < 1)
Run Code Online (Sandbox Code Playgroud)

同样,输出也是如此

println(nil > 1)
Run Code Online (Sandbox Code Playgroud)

将为false(数字1是任意的,您可以对-1执行相同操作,可能还有其他内容).我问的原因是因为我看到一些代码试图与"some_string".toInt()数值进行比较并编译,考虑到toInt()返回似乎是错误的Int?.

我的问题是,这应该是Swift中的有效语法吗?如果是这样,nil的数值是多少?


Swift 3.0更新:

看起来Swift Evolution通过删除可选的比较运算符解决了这个问题.这不再是Swift 3.0中的一个问题,因为它不能编译.

optional swift

23
推荐指数
1
解决办法
4279
查看次数

如何在使用constraintEqualToAnchor()设置它们后更改自动布局约束?

我尝试使用以下方法设置具有AutoLayout约束的视图constraintEqualToAnchor():

override func viewDidLoad() {
    super.viewDidLoad()

    let myView = UIView()
    myView.backgroundColor = UIColor.orangeColor()
    myView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(myView)

    myView.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
    myView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
    myView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
    myView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true

    /******************************************/
    /* I try to change one of the constraints */
    /******************************************/
    myView.leftAnchor.constraintEqualToAnchor(view.rightAnchor, constant: -100).active = true  
}
Run Code Online (Sandbox Code Playgroud)

在最后一行代码中,我尝试更改其中一个约束.我认为它会工作,但它在控制台日志中给出了一些错误

"<NSLayoutConstraint:0x7fb53a5180d0 H:|-(0)-[UIView:0x7fb53a5190b0](LTR)   (Names: '|':UIView:0x7fb53a519240 )>",
"<NSLayoutConstraint:0x7fb53a51f660 H:[UIView:0x7fb53a519240]-(-100)-[UIView:0x7fb53a5190b0](LTR)>",
"<NSLayoutConstraint:0x7fb53a711ee0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fb53a519240(414)]>"
Run Code Online (Sandbox Code Playgroud)

使用时constraintEqualToAnchor()?,在设置约束后,更改约束的正确方法是什么?

uiview ios autolayout nslayoutconstraint swift

18
推荐指数
1
解决办法
2万
查看次数

如何在macOS可可上使用鼠标悬停时突出显示的单元格创建表?

我正在尝试创建一个带有表的托盘弹出式应用程序,该应用程序与Dropbox的弹出式视图中的一个非常相似。有一个文件表,当您将鼠标悬停在表格单元格上时,该单元格将突出显示并显示其他控件。我不确定NSTableView是否完全适合此操作?有人有建议吗?

带高亮显示单元格的Dropbox弹出式表

macos cocoa mouseover nstablecellview

3
推荐指数
1
解决办法
604
查看次数

为什么Objective-C对象在被解除分配后仍然存在?

我这里有这个代码......

#import <Foundation/Foundation.h>
#import "Chip.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Chip *chip = [[Chip alloc] init];

    [chip release]; //Chip should be gone

    NSLog(@"%@", chip);

    [pool drain];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么在芯片发布后打印出来仍然给我描述.此时是否应该删除?

memory-leaks memory-management objective-c

2
推荐指数
1
解决办法
205
查看次数

将 NSGridView 高度设置为其内容视图的高度

我定义了一个像这样的网格视图:

let lb01 = NSTextField(labelWithString: "01")
....
let gridView = NSGridView(views:
  [ [empty, lb01],
    [empty, lb02],
    [lb03, lb04]
  ])

gridView.translatesAutoresizingMaskIntoConstraints = false  
self.addSubview(gridView)

let layoutViewMap = ["another View": anotherView, "GridView": gridView]
var constraintList = [NSLayoutConstraint]()
let constraintV = NSLayoutConstraint.constraints( withVisualFormat: "V:[GridView]-50-[anotherView]", options: [], metrics: nil, views: layoutViewMap)
constraintList += constraintV
let constraintH = NSLayoutConstraint.constraints( withVisualFormat: "|-20-[GridView(==300)]", options: [], metrics: nil, views: layoutViewMap)
constraintList += constraintH

NSLayoutConstraint.activate(constraintList)
Run Code Online (Sandbox Code Playgroud)


在某些情况下,网格视图的高度会增加

我可以定义一个明确的高度

let constraintV = NSLayoutConstraint.constraints( withVisualFormat: "V:[GridView(==70)]-50-[anotherView]", options: [], metrics: nil, views: layoutViewMap) …
Run Code Online (Sandbox Code Playgroud)

cocoa autolayout swift

1
推荐指数
1
解决办法
945
查看次数