①气泡图片设置填充方式
选中该图片->设置Slicing方式
②便利构造函数convenience
extension UIButton {// convenience : 便利,使用convenience修饰的构造函数叫做便利构造函数// 遍历构造函数通常用在对系统的类进行构造函数的扩充时使用/*便利构造函数的特点1.遍历构造函数通常都是写在extension里面2.遍历构造函数init前面需要加载convenience3.在遍历构造函数中需要明确的调用self.init()*/convenience init (imageName : String, bgImageName : String) {self.init()setImage(UIImage(named: imageName), for: .normal)setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)setBackgroundImage(UIImage(named: bgImageName), for: .normal)setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), for: .highlighted)sizeToFit()} }
③懒加载属性
// MARK:- 懒加载属性 private lazy var titleBtn : TitleButton = TitleButton()
④归档-解档对象
// MARK:- 归档&解档/// 解档的方法required init?(coder aDecoder: NSCoder) {access_token = aDecoder.decodeObject(forKey: "access_token") as? Stringuid = aDecoder.decodeObject(forKey: "uid") as? Stringexpires_date = aDecoder.decodeObject(forKey: "expires_date") as? NSDateavatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? Stringscreen_name = aDecoder.decodeObject(forKey: "screen_name") as? String}/// 归档方法 func encode(with aCoder: NSCoder) {aCoder.encode(access_token, forKey: "access_token")aCoder.encode(uid, forKey: "uid")aCoder.encode(expires_date, forKey: "expires_date")aCoder.encode(avatar_large, forKey: "avatar_large")aCoder.encode(screen_name, forKey: "screen_name")}
⑤正则匹配
/* 练习1:匹配abc练习2:包含一个a~z,后面必须是0~9 -->[a-z][0-9]或者[a-z]\d* [a-z] : a~z* [0-9]/\d : 0~9练习3:必须第一个是字母,第二个是数字 -->^[a-z][0-9]$* ^[a-z] : 表示首字母必须是a~z* \d{2,10} : 数字有2到10* [a-z]$ : 表示必须以a-z的字母结尾练习4:必须第一个是字母,字母后面跟上4~9个数字练习5:不能是数字0-9* [^0-9] : 不能是0~9练习6:QQ匹配:^[1-9]\d{4,11}$ 都是数字 5~12位 并且第一位不能是0练习7:手机号码匹配^1[3578]\d{9}$ 1.以13/15/17/18 2.长度是11 */class ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()let str = "13324132423"// 1.创建正则表达式规则let pattern = "^1[3578]\\d{9}$"// 2.创建正则表达式对象(try try? try!)guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {return}// 3.匹配字符串中内容let results = regex.matches(in: str, options: [], range: NSRange(location: 0, length: str.characters.count))// 4.遍历数组,获取结果for result in results {print((str as NSString).substring(with: result.range))print(result.range)}}}
//微博数据匹配
let statusText = "@coderwhy:【动物尖叫合辑】#肥猪流#猫头鹰这么尖叫[偷笑]、@M了个J: 老鼠这么尖叫、兔子这么尖叫[吃惊]、@花满楼: 莫名奇#小笼包#妙的笑到最后[好爱哦]!~ http://t.cn/zYBuKZ8/"// 1.创建匹配规则// let pattern = "@.*?:" // 匹配出来@coderwhy:// let pattern = "#.*?#" // 匹配话题// let pattern = "\\[.*?\\]" // 匹配表情let pattern = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?" // URL网址// 2.创建正则表达式对象guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {return}// 3.开始匹配let results = regex.matches(in: statusText, options: [], range: NSRange(location: 0, length: statusText.characters.count))// 4.获取结果for result in results {print((statusText as NSString).substring(with: result.range))}
⑥@escaping逃逸闭包
//@escaping标明这个闭包是会“逃逸”,通俗点说就是这个闭包在函数执行完成之后才被调用//没有逃逸的闭包的作用域是不会超过函数本身的,所以说我们不需要担心在闭包内持有self。逃逸的闭包就不同了,因为需要确保闭包内的成员依然有效,如果在闭包内引用self以及self的成员的话,就要考虑闭包内持有self的情况了。//@escaping标记可以作为一个警告,来提醒使用这个函数的开发者注意引用关系。func checkChildren(pageIndex: Int, childExistBlock: @escaping ()-> Void){guard let children = SCUserDefaults[.userInfo]["userChildren"] as? [Any], !children.isEmpty else{self.showNodataView(pageIndex: pageIndex)return}if nodataView != nil {nodataView.removeFromSuperview()nodataView = nil}childExistBlock()}
⑦循环引用
weak var weakSelf = selfloginVC.loginBlock = {[weak self] inweakSelf!.selectedIndex = 0self?.postNotify(name: "loginSuccess")}
⑧extendedLayoutIncludesOpaqueBars(状态栏)
self.interactivePopGestureRecognizer?.delegate = selfnavigationBar.shadowImage = UIImage()navigationBar.setBackgroundImage(image, for: .top, barMetrics: .default)navigationBar.titleTextAttributes = [.font:UIFont.boldSystemFont(ofSize: 19),.foregroundColor:UIColor.white]navigationBar.isTranslucent = false// 视图延伸不考虑透明的Bars(这里包含导航栏和状态栏)// 意思就是延伸到边界extendedLayoutIncludesOpaqueBars = true //这个属性在状态栏不透明的状态下才生效(不透明的条下可以扩展)// 意思就是空出导航栏位置//extendedLayoutIncludesOpaqueBars = falseedgesForExtendedLayout = UIRectEdge.init(rawValue: 0)self.navigationBar.tintColor = UIColor.white
⑨typealias:
用来为已存在的类型重新定义名字,支持泛型
10.Xcode配置类名前缀
选中项目->打开右边栏
11.OC引用Swift界面属性:
要在属性前+@objc
@objc var homeworkDic : HomeworkFunc?var tbView : GroupShadowTableView!@objc var isUnPublishMode : Bool = true {didSet{client.isUnPublishMode = isUnPublishMode}}
12.Swift 字典中修改某个属性的值(null替换为空字符串"")
/// 默认字典类型 typealias SCDefaultDic = [String: Any]
func saveCache(){var tempDic = dataDicif let homeworks = dataDic["homeworks"] as? [SCDefaultDic], !homeworks.isEmpty{var tempArray = homeworksfor idx in 0..<homeworks.count{var temItem = homeworks[idx]if temItem["homeworkImage"] is NSNull {temItem["homeworkImage"] = ""tempArray[idx] = temItem}}tempDic["homeworks"] = tempArray}dataDic = tempDic// 传递作业参数给 聊天室进行展示SCUserDefaults.set(self.dataDic, forKey: "isHeadMasterPublishHomework")}