2017-06-04 22 views
0

我想從一個類創建一個對象。而且我希望它能成爲應用程序的一生。我的意思是我希望它不會因爲應用程序正在運行而被釋放。
,我想從做一個實例類:在Swift中創建應用程序生命週期對象(不可能被釋放)

extension Post { 

    @NSManaged var userId: Int 
    @NSManaged var id: Int 
    @NSManaged var title: String 
    @NSManaged var body: String 

} 

class Post: NSManagedObject { 

// Insert code here to add functionality to your managed object subclass 

    override func awakeFromInsert() { 
     super.awakeFromInsert() 
     title = "" 
     userId = 0 
     id = 0 
     body = "" 

    } 
} 
+0

單身NSManagedObject子類沒有意義。 – Paulw11

+0

@ Paulw11帖子使用CoreData存儲。 –

回答

2

,保持強烈的指針本身不能被釋放的對象:

class Permanent { 
    private var ref: Permanent? 

    init() { 
     ref = self 
    } 

    deinit { 
     // This will never be called 
     print("Permanent deinit") 
    } 
} 

func test() { 
    var a = [Permanent(), Permanent()] 
    print(a.count) 

    print("free the items") 
    a = [] 
    print("end of test") 
} 

test() 

輸出:

2 
free the items 
end of test 

如果您註釋掉ref = self行:

輸出:

2 
free the items 
Permanent deinit 
Permanent deinit 
end of test 
相關問題