swift学习经验和错误记录

1、selector 和action 直接用字符串,后面要加冒号":"

2、StoryBoard 连接后改名又重新连接出现了找不到符号的诡异错误,unknow class xxxx view in interfacebuilder 删掉连接后重新连接

3、可以从StoryBoard里提取View但是这个view要是viewcontroller的rootview,如果不是rootview可能会崩溃

4、自定义xib在view出写上自定义类,而不是file owener

5、controller切换时,show可以继承上一个的导航栏,而present不会

6、修改UISegmentControl 的字体设置

        let attrib = [NSFontAttributeName: UIFont.boldSystemFontOfSize(16), NSForegroundColorAttributeName:UIColor.blackColor()]

        segControl!.setTitleTextAttributes(attrib, forState: UIControlState.Normal)

        segControl!.setTitleTextAttributes(attrib, forState: UIControlState.Selected)

7、使用autolayout事,tableview上有时候会有一个空白,可以让tableview得上部对齐由top layou 改为super view

8、 iOS Xcode, 解决“Could not insert new outlet connection”的问题,参考此文,还有一种说法是对应的类没有加到target里,不过这种情况很少。

9、NSURL(string: str)为nil原因因为str里面有中文,所以NSURL无法识别增加编码格式

str = (str as NSString).stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)

10、UICollectionView的注意事项

  a、要collectionView.registerClass(ScheduleCell.classForCoder(), forCellWithReuseIdentifier: identifier)

  b、点击时获取cell

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath) as ScheduleCell

  //不能用 collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as ScheduleCell 否则都是新建的了,至于原因不知,还望大侠赐教

    }

11、ios uitextfield uitextinput 不弹出软键盘  模拟器设置里面 Hardware - keyboard 内设置了 链接电脑键盘。你可以点击第三个:toggle software keyboard让虚拟机弹出,或者把 connect hardware keyboard 不勾选,这样就能自动弹出虚拟键盘了(不过电脑键盘就输入不了了) 

12、transform 时如果 点在下边,向下拖动,会先向上移动一下。

13、w、h不同storyboard会有不同的配置,切换不同的配置,会变灰

14、Swift 默认参数和外部名一致可以不用#,如果是inout参数用&

注意整型Int32 和Int的区别

原文地址:https://www.cnblogs.com/xdao/p/swift_ios_experience.html