赞
踩
int *array = malloc(20000 * sizeof(int));
array[0] = 32
array[19999] = 64
-理解了 iOS 系统层面上的内存机制,在系统层面上的内存管理大多数情况下都已经由操作系统自动完成了。iOS 中一个 App 就是一个进程,所以开发者平时经常讨论的内存管理,比如 MRC、ARC 等等,实际上属于进程内部的内存管理,或者说是语言层面上的内存管理。这部分内存管理语言本身、操作系统均会有一些管理策略,但是作为开发者来说,很多时候还是需要从语言层面直接进行操作的。
class viewController: UIViewController { var a = 10 var b = 20 var someClosure: (() -> Int)? func anotherFunction(closure: @escaping () -> Int) { DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5)) { print(closure) } } override func viewDidLoad() { super.viewDidLoad() someClosure = { return self.a + self.b } anotherFunction(closure: someClosure!) } }
someClosure = { [weak self] in
guard let self = self else { return 0 }
return self.a + self.b
}
someClosure = { [unowned self] in
return self.a + self.b
}
Fatal error: Attempted to read an unowned reference but the object was already deallocated
Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime.
class Customer { let name: String var card: CreditCard? init(name: String) { self.name = name } deinit { print("\(name) is being deinitialized") } } class CreditCard { let number: UInt64 unowned let customer: Customer init(number: UInt64, customer: Customer) { self.number = number self.customer = customer } deinit { print("Card #\(number) is being deinitialized") } }
If the captured reference will never become nil, it should always be captured as an unowned reference, rather than a weak reference.
class HTMLElement { let name: String let text: String? lazy var asHTML: () -> String = { [unowned self] in if let text = self.text { return "<\(self.name)>\(text)</\(self.name)>" } else { return "<\(self.name) />" } } init(name: String, text: String? = nil) { self.name = name self.text = text } }
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.execute()
}
class APIClass { // static 函数 static func getData(params: String, completion:@escaping (String) -> Void) { request(method: .get, parameters: params) { (response) in completion(response) } } } class viewController { var params = "something" var value = "" override func viewDidLoad() { super.viewDidLoad() getData(params: self.params) { (value) in self.value = value } } }
static const char * memorystatus_priority_band_name(int32_t priority) { switch (priority) { case JETSAM_PRIORITY_FOREGROUND: return "FOREGROUND"; case JETSAM_PRIORITY_AUDIO_AND_ACCESSORY: return "AUDIO_AND_ACCESSORY"; case JETSAM_PRIORITY_CONDUCTOR: return "CONDUCTOR"; case JETSAM_PRIORITY_HOME: return "HOME"; case JETSAM_PRIORITY_EXECUTIVE: return "EXECUTIVE"; case JETSAM_PRIORITY_IMPORTANT: return "IMPORTANT"; case JETSAM_PRIORITY_CRITICAL: return "CRITICAL"; } return ("?"); }
// 同样在 bsd/kern/kern_memorystatus.c 文件中
result = kernel_thread_start_priority(memorystatus_thread, NULL, 95 /* MAXPRI_KERNEL */, &jetsam_threads[i].thread);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。