This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension AppDelegate { | |
/// 获取 App delegate | |
static func mainDelegate() -> AppDelegate? { | |
if let delegate = UIApplication.shared.delegate as? AppDelegate { | |
return delegate | |
} | |
return nil | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension UIDevice { | |
/// 判断当前设备时间是否是12小时制? | |
static func is12HourMode() -> Bool{ | |
if let formatStringForHours = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current) { | |
return formatStringForHours.contains("a") | |
} | |
return false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 需要 #import <CommonCrypto/CommonDigest.h> | |
// 对字符串进行MD5加密 | |
+ (NSString *)MD5WithString:(NSString *)string | |
{ | |
const char *cStr = [string UTF8String]; | |
unsigned char digest[CC_MD5_DIGEST_LENGTH]; | |
CC_MD5( cStr, (uint32_t)strlen(cStr), digest); | |
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public extension UIView { | |
/// 添加阴影效果 | |
func setShadow(color: UIColor = UIColor.black, opacity: Float = 0.5, offset: CGSize = CGSize(width: 0, height: 0)) { | |
// 添加阴影 | |
self.layer.shadowColor = color.cgColor | |
// 阴影的透明度 | |
self.layer.shadowOpacity = opacity | |
// 阴影的偏移 | |
self.layer.shadowOffset = offset | |
// 避免离屏渲染带来卡顿 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-------------------------------------------------------- define Model-------------------------------------------------- | |
import Foundation | |
class TodoItem: NSObject, NSCoding { | |
// Attribute of model | |
var name: String = "" | |
var isFinished: Bool = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
// Get navigation controller | |
let naviController = segue.destination as! UINavigationController | |
// Get view controller | |
let todoDetailController = naviController.topViewController as! TodoDetailTableViewController | |
if segue.identifier == "AddTodo" { | |
todoDetailController.title = "Add todo" | |
// Todo | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
附:NSDateFormatter格式化参数如下: | |
G: 公元时代,例如AD公元 | |
yy: 年的后2位 | |
yyyy: 完整年 | |
MM: 月,显示为1-12 | |
MMM: 月,显示为英文月份简写,如 Jan | |
MMMM: 月,显示为英文月份全称,如 Janualy | |
dd: 日,2位数表示,如02 | |
d: 日,1-2位显示,如 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 计算今年年龄 eg: 1992-03-19 | |
func calculateThisYearAge(birthday: String) -> Int?{ | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd" | |
if let birthday = dateFormatter.date(from: birthday) { | |
let gregorian = Calendar(identifier: .gregorian) | |
let components = gregorian.dateComponents([Calendar.Component.year], from: birthday, to: Date()) | |
if let year = components.year { | |
return year + 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 正向传值 | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if let identifier = segue.identifier { | |
if identifier == "ToBeauty" { | |
let desVC = segue.destination as! BeautyViewController | |
let index = pickerView.selectedRow(inComponent: 0) | |
desVC.imageName = imageNames[index] | |
desVC.name = names[index] | |
} |
OlderNewer