Swift 杂谈(一)

2018-03-30  本文已影响22人  目染江夏

1,override 修饰方法 是系统方法

//会自动加上这个方法

required init?(coder aDecoder: NSCoder) {

    fatalError("init(coder:) has not been implemented")

}

2,@objc 使用@objc修饰后的类型,可以直接供 Objective-C 调用

可用 @objc 修饰的

未嵌套的类

协议

非泛型枚举(仅限于原始值为整形的类型)

类和协议中的属性和方法

构造器和析构器

下标

3,委托代理

1)声明代理方法 optional 修饰可以不必实现

//属性代理

@objc protocol CycleScrollViewDelegate : NSObjectProtocol{

// MARK: - 点击图片回调

//optional 修饰可以不必实现

@objc optional  func cycleScrollViewDidSelectionItemAtIndex(cycleScrollView:CYCycleScrollView , index:Int)

// MARK: - 图片滚动回调

@objc optional func

    cycleScrollViewDidScrollToIndex(index:Int)

}

2)声明代理对象 在@class中

weak var delegate: CycleScrollViewDelegate?

3)签协议 实现代理方法,不用optional修饰的代理方法必须实现,不实现会报错

class ViewController: UIViewController,CycleScrollViewDelegate {}

4,collectionview ,tableview 创建

//注册cell

//CYCollectionViewCell :自定义cell 类

//classForCoder() 获取该类的类型

mainView.register(CYCollectionViewCell.classForCoder(), forCellWithReuseIdentifier:"CYCollectionViewCell”)

//获取cell

//在设置cell的代理方法中

let cell:CYCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CYCollectionViewCell", for: indexPath) as! CYCollectionViewCell

5,swift与OC混编

在swift中使用 sdwebImage

swift 中使用 sdwebview 需要用桥接

添加一个Object-c和swift的桥接,新建一个头文件,名叫SDWebImage+Header.h ,添加SDWebImage头文件的引用,

Bulid Settings 中

添加bridging header $(SRCROOT)/ 根目录

image.png

Bulid Settings 中

Other Link flags处添加 -ObjC

image.png

6,print

  1. // 直接使用变量名称输出

  2. let lastName = "Zhang"

  3. print(lastName)

  4. // 在字符串中以参数形式输出

  5. print("你的名称是:(firstName)(lastName)")

7,for 循环

// index 索引 value 索引对应value

for (index,value) in arr.enumerated(){}

8,selector方法

selector(self.automaticScroll)

9,懒加载

//懒加载 showScrollview

lazy var showScrollView:UIScrollView = {

    () -> UIScrollView in

    let showScrollView = UIScrollView()

    return showScrollView

}()

10,类方法

在类方法前面加上 class 或 staic 但是类方法 class、staic只能使用一个

用staic 修饰

六种修饰符 表示权限:

1)private

只允许在当前类中调用,不包括Extension(扩展)

不可以被代码域之外的地方访问

2)fileprivate

当前文件可以访问

3)public

可以再其他作用域被访问

不能在重载override 中访问

不能在继承方法中的Extension中被访问

4)open

可以再其他作用域被访问

可以在其他作用域被继承或重载override

5)final

在任何地方都不能override

6)internal(默认访问级别,internal修饰符可写可不写)

internal访问级别所修饰的属性或方法在源代码所在的整个模块都可以访问。

如果是框架或者库代码,则在整个框架内部都可以访问,框架由外部代码所引用时,则不可以访问。

如果是App代码,也是在整个App代码,也是在整个App内部可以访问。

上一篇下一篇

猜你喜欢

热点阅读