1.引文文件
import Kingfisher
@_exported import MJRefresh
@_exported import 加入头文件之前的话,就是全局引用
2.自定义UIView或者cell 设置图片圆角
override func layoutSubviews() {
super.layoutSubviews()
userHeadbBtn.clipRectCorner(direction: .allCorners, cornerRadius: 50/2)
mainView.clipRectCorner(direction: .allCorners, cornerRadius: 10)
mainView.addShadowToView(color: UIColor.black)
}
自定义的组件里如果需要设置圆角、设置阴影等产生效果必须在layoutSubviews 或者awakeFromNib中调用方法才能有效果
3.?(可选类型)和!(隐式可选类型)的区别
? 在判断的类型为空时 不会让 程序闪退,甚至可以在?后给 变量初始一个值。比如 textString? : "aaaa"。
!号会让程序默认改变量强制不为空,如果不幸为空了,程序直接闪退
4.存储用户信息归档解档
1.用户数据模型需要遵循NSObject, HandyJSON,NSSecureCoding三种协议
import UIKit
class LoginModel: HandyJSON {
var statusCode: Int?
var message: UserInfoModel?
required init() {}
}
class UserInfoModel: NSObject, HandyJSON,NSSecureCoding {
static var supportsSecureCoding: Bool { return true }
var userAccount:Int?
var userID:String?
var userName:String?
var userPhone:String?
var userPhoto:String?
var userToken:String?
var userType:String?
required override init() {}
func encode(with aCoder: NSCoder) {
aCoder.encode(userAccount, forKey: "userAccount")
aCoder.encode(userID, forKey: "userID")
aCoder.encode(userName, forKey: "userName")
aCoder.encode(userPhone, forKey: "userPhone")
aCoder.encode(userPhoto, forKey: "userPhoto")
aCoder.encode(userToken, forKey: "userToken")
aCoder.encode(userType, forKey: "userType")
}
required init?(coder aCoder: NSCoder) {
userAccount = aCoder.decodeObject(forKey: "userAccount") as? Int
userID = aCoder.decodeObject(forKey: "userID") as? String
userName = aCoder.decodeObject(forKey: "userName") as? String
userPhone = aCoder.decodeObject(forKey: "userPhone") as? String
userPhoto = aCoder.decodeObject(forKey: "userPhoto") as? String
userToken = aCoder.decodeObject(forKey: "userToken") as? String
userType = aCoder.decodeObject(forKey: "userType") as? String
}
}
2.定义UserDefaults存储类
import Foundation
private let USER_DEFAULTS = UserDefaults.standard
private let USERINFO_DEFAULT_KEY = "UserInfoModel"
//Mark -----用户信息保存数据
func saveUserInfoModel(model: UserInfoModel){
var encodedObject: Data?
if #available(iOS 11.0, *) {
if let object = try? NSKeyedArchiver.archivedData(withRootObject: model, requiringSecureCoding: true) {
encodedObject = object
}
} else {
encodedObject = NSKeyedArchiver.archivedData(withRootObject: model)
}
if (encodedObject != nil) {
USER_DEFAULTS.set(encodedObject, forKey: USERINFO_DEFAULT_KEY)
USER_DEFAULTS.synchronize()
}
}
//Mark -----用户信息读取数据
func getuserInfoModel() -> UserInfoModel? {
if let decodedObject = USER_DEFAULTS.object(forKey: USERINFO_DEFAULT_KEY) as? Data {
return try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decodedObject) as? UserInfoModel
}else{
return nil
}
}
//Mark -----清除对象数据
func clearUserInfoModel () {
USER_DEFAULTS .removeObject(forKey: USERINFO_DEFAULT_KEY)
USER_DEFAULTS.synchronize()
}
注意: let encodedObject = try NSKeyedArchiver.archivedData(withRootObject: model, requiringSecureCoding: true) 和 let object = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decodedObject as! Data) as? UserInfoModel 方法需要匹配对应使用
5.SwiftyUserDefaults使用
1.存储数据
import Foundation
extension DefaultsKeys {
var APPToken: DefaultsKey<String?> { .init("APPToken") }
}
Defaults[\.APPToken] = "1111"
2.取数据
let token = Defaults[\.APPToken]
print("name is \(Defaults[\.APPToken])")
3.清除数据
//清除部分
Defaults.remove(\.APPToken)
// 清除全部
Defaults.removeAll()
6.Swift 自定义的UIView 获取父控制器
extension MineHeadView{
func getSupperViewController()->UIViewController?{
for view in sequence(first: self.superview, next: {$0?.superview}){
if let responder = view?.next{
if responder.isKind(of: UIViewController.self){
return responder as? UIViewController
}
}
}
return nil
}
}
let loginVc = LoginViewController()
getSupperViewController()?.navigationController?.pushViewController(loginVc, animated: true)
7.Swift Kingfisher 获取URL地址图片
1.button获取URL图片
if let url = URL(string: userInfoModel.userPhoto ?? "") {
mineHeadView.userHeadbBtn.kf.setImage(with: url, for: .normal)
}
2.UIimageView获取URL图片
//有占位图
if let url = URL(string: "http://mvimg2.meitudata.com/55fe3d94efbc12843.jpg") {
imageView.kf.setImage(with: url, placeholder: placeholder_image, options: nil, progressBlock: nil, completionHandler: nil)
}
//无占位图
if let url = URL(string: "http://mvimg2.meitudata.com/55fe3d94efbc12843.jpg") {
imgView.kf.setImage(with: url)
}
8.Swift Kingfisher URL过滤器
直接传入url图片地址即可过滤
import Foundation
import Kingfisher
//kfininsher 图片过滤器
func KfnisherImageResource(urlString: String) -> Resource {
guard urlString.isBlank == false else {
return "" as! Resource
}
let url = URL(string: urlString)
let newUrl = NSURL.init(scheme: url?.scheme ?? "",host: url?.host ?? "", path: url?.path ?? "")
return ImageResource(downloadURL: URL(string: urlString)!, cacheKey: newUrl?.relativeString)
}
9. Swift-try处理异常的三种方式
方式一:try方式 程序员手动捕捉异常
do {
try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
} catch {
// error异常的对象
print(error)
}
方式二:try?方式(常用方式) 系统帮助我们处理异常,如果该方法出现了异常,则该方法返回nil.如果没有异常,则返回对应的对象
guard let anyObject = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) else {
return
}
方式三:try!方法(不建议,非常危险) 直接告诉系统,该方法没有异常.注意:如果该方法出现了异常,那么程序会报错(崩溃)
let anyObject = try!NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers)
10. Swift-func 的几种修饰类型
1.重写方法
//@final 是防止被重写
@final func configure() {
}
//父类里
func configure() {
}
//子类里
override func configure() {
}
2.Swift语言中,访问修饰符有五种,分别为fileprivate,private,internal,public和open,这些修饰func 或者class 都可以被重写,可以访问func和class里面的属性 。private修饰方法和属性,则为私有方法和属性
3.如果子类需要添加异于父类的初始化方法时,必须先要实现父类中使用required修饰符修饰过的初始化方法,并且也要使用required修饰符而不是override
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
4.static修饰的类方法不能继承;class修饰的类方法可以继承,class不能修饰类的存储属性,static可以修饰类的存储属性
class MyClass {
class func testFunc() {
}
static func testFunc1() {
}
}
class MySubClass: MyClass {
override class func testFunc() {
}
// error: Cannot override static method
// override static func testFunc1() {
//
// }
}
5.如果父控制器分类中的方法,需要在子控制器中重写,直接重写会报错:不支持从扩展名覆盖非@objc声明(Overriding non-@objc declarations from extensions is not supported),需要在父控制器方法前加上@objc
//设置导航栏
@objc func setupNavBar()
{
navigationItem.titleView = tittleLabel
self.isHidenNaviBarLowerLine = false
}
6.struct 修饰的结构体 ,class 修饰的类
11. 子view中点击事件在父控制器中调用点击事件,通过闭包回调解决
1.在子view 中创建闭包
extension FootView{
// 闭包 类似oc中的block
static var submitBtnCallBack:(() -> ())?
//按钮所指向的函数,其中包含闭包
//提交按钮
@objc func submitBtnClick() {
Self.submitBtnCallBack?()
}
}
2.在控制器中闭包回调
// MARK: - 底部foodview 闭包(block)的回调
extension FeedBackViewController{
func blockCallBack(){
// 闭包(block)的回调
FootView.submitBtnCallBack = { () -> () in
SHOW_ALERT(msg: "122")
}
}
}
12. 多张图片上传,等待图片所有上传结束再执行下一步
var myGroup = DispatchGroup()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0 ..< 5 {
myGroup.enter()
Alamofire.request("https://httpbin.org/get", parameters: ["foo": "bar"]).responseJSON { response in
print("Finished request \(i)")
self.myGroup.leave()
}
}
myGroup.notify(queue: .main) {
print("Finished all requests.")
}
}
13. Swift数组字符串互转
let str003 = "a,b,c,d,e,f"
let list001 = str003.split(separator: ",")
print(list001)
let str004 = list001.joined(separator: ",")
print(str004)
14. Swift数组添加另外一个数组
var first = ["John", "Paul"]
let second = ["George", "Ringo"]
//三种方法
first.append(contentsOf: second)
first += second
let third = first + second
网友评论