lazy var imageView:UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage.init(named: "big_image")
return imageView
}()
lazy var button:UIButton = {
let button = UIButton()
button.setTitleColor(UIColor.red, for: .normal)
button.backgroundColor = UIColor.white
button.layer.cornerRadius = 2.0
button.layer.masksToBounds = true
button.isHidden = true
button.addTarget(self, action: #selector(buttonClick(_ :)), for: .touchUpInside)
return button
}()
@objc func buttonClick(_ button:UIButton) -> () {
}
protocol ImageViewDelegate: class {
func selectedImage() -> ()
func removeImage(_ index: Int) -> ()
}
@objc protocol ImageCellDelegate: NSObjectProtocol {
@objc optional func imageCelllLikeButtonClick(imageCell: ImageCell, commentId: String, userLiked:Bool) -> ()
@objc optional func imageCellDetalButtonClick(imageCell: ImageCell, content:String) -> ()
}
weak var delegate:ImageCellDelegate?
注意:
代理继承的父类 class和NSObjectProtocol 的区别
代理继承的父类NSObjectProtocol的时候,判断代理是否实现某个方法responds(to: #selector()能提示处理,继承自class则没法判断
ImageCellDelegate.实现的方法去判断!!!
@objc func buttonClick(_ button:UIButton) -> () {
guard let delegate = delegate else { return }
if delegate.responds(to: #selector(ImageCellDelegate.imageCellButtonClick(imageCell:content:))) {
delegate.imageCellButtonClick!(imageCell: self, content: commentLabel.text)
}
}
//判断某个类
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isMember(of: tableView.classForCoder) {
view.endEditing(true)
}
}
//判断某个类及其子类
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isKind(of: tableView.classForCoder) {
view.endEditing(true)
}
}
//刷新所有
self.tableView.reloadData()
//刷新分组
self.tableView.reloadSections([1], with: .none)
//刷新某一个cell或者几个cell
self.tableView.reloadRows(at: [indexPath], with: .none)
class func getTopicContentImageUrlLink(_ range: NSRange, _ pattern: String = "", _ content: String) -> [URL] {
var imageUrlArr: [URL] = []
//利用正则匹配
let pattern = pattern
guard let regularExpression = try? NSRegularExpression.init(pattern: pattern, options: .dotMatchesLineSeparators) else { return []}
let regularResults = regularExpression.matches(in: content, options: [], range: NSMakeRange(0, content.count))
for textCheckingResult in regularResults {
//查找到的范围数量
// 0: 和匹配方案完全一致的字符串
// 1: ()中需要匹配的字符串
for idx in 0..let range = textCheckingResult.rangeAt(idx)
let subStr = (content as NSString).substring(with: range)
let url = URL(string: subStr)
if idx == 1 && url != nil {
imageUrlArr.append(url!)
}
}
}
return imageUrlArr
}
let content = (newString as NSString).addingPercentEscapes(using: String.Encoding.utf8.rawValue)!
//线程通信
DispatchQueue.global().async {
//do async something
DispatchQueue.main.async {
//main thread do UI update
}
}
//延迟操作 DispatchTime 的精度是纳秒
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 10) {
}
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 10) {
}
//DispatchWallTime 的精度是微秒
DispatchQueue.global().asyncAfter(wallDeadline: DispatchWallTime.now() + 2.0) {
}
let arr = [1,2,3,4,5]
//是个切片数组和数组之间可以转化
let arrSlice = arr[0..<3]
let subArr = Array(arrSlice)
//通知名称
let ZQPCouponCellSelectedCoupon: Notification.Name = Notification.Name(rawValue: "ZQPCouponCellSelectedCoupon")
//发通知
NotificationCenter.default.post(name: ZQPCouponCellSelectedCoupon, object: couponModel)
//接通知
NotificationCenter.default.addObserver(self, selector: #selector(selectedCoupon(_ :)), name: ZQPCouponCellSelectedCoupon, object: nil)
//响应事件
@objc func selectedCoupon(_ notication: Notification) {
let couponModel = notication.object as! ZQPPlanPlayRedEnvelopeModel
}
//当前时间---毫秒级 时间戳 - 13位
let timeInterval: TimeInterval = Date().timeIntervalSince1970
let currentMillisecond = CLongLong(round(timeInterval*1000))
print(currentMillisecond)
`1532068608613`
//当前时间---秒级 时间戳 - 10位
let timeInterval: TimeInterval = Date().timeIntervalSince1970
let currentSecond = Int(timeInterval)
print(currentSecond)
`1532068723`
属性前+@objc,否则字典转模型不成功
class PersonModel: NSObject {
//姓名
@objc var name: String?
//年龄
@objc var age: Int = 0
}
static let sharedInstance = ZYManagerController()
//OC
#import
//Swift
import UIKit.UIGestureRecognizerSubclass
//注意,确保string有值,否则每一步转换,都要guard防卫
let childrenControllerString: String = item[RootVcName] as! String
let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
let childrenControllerClass = NSClassFromString(nameSpace + "." + childrenControllerString)
let childrenControllerType = childrenControllerClass as! UIViewController.Type
let childrenController = childrenControllerType.init()