Swift 编程杂谈

时间:2023-03-08 22:37:04

1、Swift 3.0 使用Cocopods 导入第三方报错

  之前一直用Object-C 编写代码  用Cocopods导入第三方没出过什么问题(PS:2017最新cocoaPods安装教程

  今天用Swift写项目 导入第三方的时候出现这个错误:

  [!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfile or target to opt into using it. The swift Pods being used are: ExSwift

后来查证,需要在Podfile文件中加入use_frameworks!

之前是这样写的:

platform :ios, ’10.3’
target ‘MySwiftDemo’ do
pod 'ExSwift', '~> 0.1.9'
end

加上 use_frameworks! 问题解决

platform :ios, ’10.3’
target ‘MySwiftDemo’ do
pod 'ExSwift', '~> 0.1.9'
use_frameworks!
end


2、重写UINavigationController 返回按钮 backBarButtonItem&leftBarButtonItem

  项目中很多地方要重写返回按钮样式或者要在返回事件中进行一些操作,这就要重写backBarButtonItem或者leftBarButtonItem两个属性。

  ①、对当前页面而言,leftBarButtonItem的优先级最高,如果对当前页面的self.navigationItem.leftBarButtonItem赋新值,则当前页面每次被push进来左上角显示的就是这个新的UIBarButtonItem。

  ②、如果当前页面的self.navigationItem.leftBarButtonItem没有赋新值,但是push当前页面进来的上个页面的self.navigationItem.backBarButtonItem 重新赋值了,则当前页面的左上角显示上个页面的self.navigationItem.backBarButtonItem。

  ③、如果当前页面的leftBarButtonItem和上个页面的backBarButtonItem都没有赋新值,则当前页面的左上角显示默认的返回按钮,一个向左的箭头和上个页面导航栏的title。

  注意:针对第②种情况,给 self.navigationItem.backBarButtonItem   赋新值的时候,创建 UIBarButtonItem 时,使用:

public convenience init(barButtonSystemItem systemItem: UIBarButtonSystemItem, target: Any?, action: Selector?)

public convenience init(customView: UIView)

  这两种方法都是无效的(亲测无效,知道原因的小伙伴可以告知我),下个页面的左上角的返回按钮仍会按照第三种情况的样式显示。

  使用:

public convenience init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)

  创建时,会同时显示两个图片,左边的是系统默认向左的箭头(默认蓝色),右边的是你添加的图片

  使用:

public convenience init(title: String?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)

  创建时,依然会显示系统默认的向左的箭头,右边的文字显示的是创建时的title。(如果title为nil 则只显示系统默认的箭头)

   补充:leftBarButtonItem的优先级高于backBarButtonItem,如果当前页面的leftBarButtonItem被赋新值,push当前页面的backBarButtonItem也赋新值,则当前页面左上角返回按钮显示leftBarButtonItem!!


3、在stryboard修改navigationBar的属性(如:barTintColor、titleColor等)会出现警告!!

  警告内容如下:

  Frame for "Navigation Bar" will be different at run time.

  Width will be 320 at run time but is 375 in the canvas.

Swift 编程杂谈

意思是视图在运行时宽度会改变(Width由375变为320),我试图点击自动布局进行修改,却发现修改不了,后来在*找到答案,需要修改Navigation Controller的Simulated Size为Freeform即可。

Swift 编程杂谈


4、使用cocopods时,pod install和pod update什么时候用

  许多人开始使用CocodPods的时候认为pod install只是你第一次用CocoaPods建立工程的时候使用,而之后都是使用pod update,但实际上并不是那会事。

简单来说,就是:

  ①、使用pod install来安装新的库,即使你的工程里面已经有了Podfile,并且已经执行过pod install命令了;所以即使你是添加或移除库,都应该使用pod install。

  ②、使用pod update [PODNAME] 只有在你需要更新库到更新的版本时候用。


5、Swift中创建Swift File文件编写全局变量无效

  OC中常用的变量我们都是写在pch文件中,可是Swift中搭建ProjectName-Bridging-Header.h(OC和Swift混编)文件引入pch文件并没有效果,所以我们需要另辟蹊径。

  解决方法:

  Command+N创建Swift File文件,将常用的全局变量写在这个Swift文件中,Swift中不需要对.h文件的引入,如果项目需要混编,头文件的引入写在Bridging-Header.h中。这时候问题来了,我穿件好Swift File文件,写入全局变量却不能在其他文件中调用,很是恼火!

Swift 编程杂谈

Swift 编程杂谈

Swift 编程杂谈

Swift 编程杂谈

  最后得到解决,需要将创建的Swift File文件与调用全局变量的文件放在同一个根文件下!


6、关键字static和class的区别

  在方法的func关键字之前加上关键字static或者class都可以用于指定类方法. 不同的是用class关键字指定的类方法可以被子类重写, 如下:

override class func work() {
print("Teacher: University Teacher")
}

但是用static关键字指定的类方法是不能被子类重写的

根据报错信息: Class method overrides a 'final' class method. 我们可以知道被static指定的类方法包含final关键字的特性--防止被重写.

类方法和实例方法可以重名,但不建议写相同的方法名.


7、Swift_修饰符(final、override、discardableResult、mutating、lazy、inout.....)

 ①、final 防止重写

//防止重写
class finalClass{
final var applePen = 1
final func test(){ } final class func test2(){ }
} class finalSub: finalClass {
//无法重写基类 定义了 final 的属性和函数
} //加到class前,无法被继承
final class unDo{ }

②、@discardableResult

@discardableResult //取消如果不使用返回值的警告
class func unUseReuslt(d x:Int) -> Bool { return true
}

③、mutating

实例化的时候,实参是不可改变,如果确实需要改的话加 mutating

struct Point {
var x = 0, y = 0 mutating func moveXBy(x:Int,yBy y:Int) {
self.x += x
self.y += y
}
} var p = Point(x: 5, y: 5) p.moveXBy(3, yBy: 3)

另外,在值类型的实例方法中,也可以直接修改self属性值。

enum TriStateSwitch {
case Off, Low, High
mutating func next() {
switch self {
case Off:
self = Low
case Low:
self = High
case High:
self = Off
}
}
}
var ovenLight = TriStateSwitch.Low
ovenLight.next()
// ovenLight is now equal to .High
ovenLight.next()
// ovenLight is now equal to .Off”

④、convenience便利构造器

// 指定构造器 和 便利构造器
class Food {
var name: String
init(name: String) {
self.name = name
}
//便利构造器
convenience init() {
self.init(name: "[Unnamed]")
} }

⑤、lazy懒加载

Object-C的懒加载是当使用时才去开辟空间,set get方法

Swift 懒加载相当于静态空间,只运行一次....只打印一次 a = 1

lazy var num: Int = {
print("只打印一次!")
return 1
}() print(num)
num += num
print(num) // 只打印一次!
// 1
// 2

⑥、Swift关键字inout - 让值类型以引用方式传递

⑦、defer关键词,让代码块延期执行