Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

ios - App and Extension - Use Core data == error : sharedApplication()' is unavailable

I'm trying to read/write data in extra class file which is public. (StoreData.swift)

My function :

  func FetchName (NameforDate: String) -> NSString
         {
             var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
             var context:NSManagedObjectContext = appDel.managedObjectContext! 
    }

At line where is "(UIApplication.sharedApplication()." I get the error:

StoreData.swift:115:49: 'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.

I created App and Today Extension.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need a managed object context you can create your own within your extension.Your AppDelegate will not be available to you. I would suggest moving out that from your app delegate to a class you can share between both. You will still be able to listen for core data updates so they don't need to share contexts/persistent store coordinates.

EDIT:

I'd like to point out that extensions are essentially a different sandbox. Therefore if you create a shared class the instances will not be the same shared between App and Extension. Therefore making the persistent store coordinator and contexts different.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...