zoukankan      html  css  js  c++  java
  • 第九篇、Swift的基本使用

    1.访问权限

    /*
     1> internal : 内部的
        1. 默认情况下所有的类&属性&方法的访问权限都是internal
        2. 在本模块(项目/包/target)中可以访问
     2> private : 私有的
        1. 只有在本类中可以访问
     3> open : 公开的
        1. 可以跨模块(项目/包/target)都是可以访问
     4> fileprivate : Swift3.0
        1. 只要是在本文件中都是可以进行访问
     */

     

    2.去掉xcode8的日志打印:Edit->Run->ENvironment variables -->添加OS_ACTIVITY_MODE 设置值为disable

      common 加 = 让图片大小按尺寸适应 (快捷键)

      sudo /usr/libexec/xpccachectl  + 重启电脑     因为xcode8的注释不可以用了

      @objc private 修饰的方法(为了保留OC的特性,方法能正常的响应)

    3.字符串的截取

     Swift中提供了特殊的截取方式

    • 该方式非常麻烦
    • Index创建较为麻烦
    • 简单的方式是将String转成NSString来使用
      • 在标识符后加:as NSString即可
    let urlString = "http://www.baidu.com"
    // Swift中通过 as 关键字可以将String类型转成NSString的类型
    let header1 = (urlString as NSString).substring(to: 3)
    let footer1 = (urlString as NSString).substring(from: 10)
    let range1 = NSRange(location: 4, length: 5)
    let middle1 = (urlString as NSString).substring(with: range1)

    swift的截取方式:

    let urlString = "www.baidu.com"
    let headerIndex = urlString.index(urlString.startIndex, offsetBy: 3)
    let header2 = urlString.substring(to: headerIndex)
    let footerIndex = urlString.index(urlString.endIndex, offsetBy: -3)
    let footer2 = urlString.substring(from: footerIndex)
    let startIndex = urlString.index(urlString.startIndex, offsetBy: 4)
    let endIndex = urlString.index(urlString.startIndex, offsetBy: 9)
    let range2 = Range(startIndex..<endIndex)
    let middle2 = urlString.substring(with: range2)

    4、解决循环引用

    swift中解决循环引用的方式
    方案一:
    使用weak,对当前控制器使用弱引用
    但是因为self可能有值也可能没有值,因此weakSelf是一个可选类型,在真正使用时可以对其强制解包(该处强制解包没有问题,因为控制器一定存在,否则无法调用所在函数)
        // 解决方案一:
        weak var weakSelf = self
        httpTool.loadData {
            print("加载数据完成,更新界面:", NSThread.currentThread())
            weakSelf!.view.backgroundColor = UIColor.redColor()
        }
    方案二:
    和方案一类型,只是书写方式更加简单
    可以写在闭包中,并且在闭包中用到的self都是弱引用
        httpTool.loadData {[weak self] () -> () in
            print("加载数据完成,更新界面:", NSThread.currentThread())
            self!.view.backgroundColor = UIColor.redColor()
        }
    方案三:(常用)
    使用关键字unowned
    从行为上来说 unowned 更像OC中的 unsafe_unretained
    unowned 表示:即使它原来引用的对象被释放了,仍然会保持对被已经释放了的对象的一个 “无效的” 引用,它不能是 Optional 值,也不会被指向 nil
    httpTool.loadData {[unowned self] () -> () in
            print("加载数据完成,更新界面:", NSThread.currentThread())
            self.view.backgroundColor = UIColor.redColor()
        }
  • 相关阅读:
    DNNClassifier 深度神经网络 分类器
    浏览器对MP4视频 帧宽度 高度的兼容性
    UnicodeEncodeError:'latin-1' codec can't encode character
    文件夹下 文件计数
    the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
    the “identity” of an object
    广告特征 用户特征
    如果一个维度全覆盖,则有效维度应该对该维度全覆盖
    a high-level neural networks AP
    使用 LDA 挖掘的用户喜好主题
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5917488.html
Copyright © 2011-2022 走看看