Adam
Adam

Reputation: 1913

Swift How to fix errors with Target Membership?

I am beginner to Swift and I am not aware of all the little details, so please forgive if it's a stupid question

My app is in quite advanced stage of development, so I would like to not mess with the existing code as little as possible. My problem is that I want to implement Today Extension in my app, in it I want to display a tableView that will display data from a single array from my "Plum" class and I need one method from the class to handle the selection. Unfortunately when I change the Plum's target membership to TodayExtension a whole bunch of errors are shown because many APIs from that class aren't available to TodayExtension. The class inherits from AVAudioPlayer so most essential methods aren't available in Extension. I tried to create a helper class that would get the array from Plum and return it in method to my TableView but that still doesn't work because of "Use of unresolved identifier 'Plum'"

How can I obtain the array and use the method?

Upvotes: 1

Views: 741

Answers (2)

Surya Subenthiran
Surya Subenthiran

Reputation: 2217

Adding the Plum class in the today extension is a bad idea. Extensions should be lightweight and you only need to transfer the data needed.

In your case you may need the array of title and description. So form a array of dictionary with needed details and store these details in App group user defaults like @Hitesh answer.

In your today extension class, read this array from the user defaults then display the details in the table view.

Upvotes: 0

Hitesh Sultaniya
Hitesh Sultaniya

Reputation: 939

I think while adding Today Extension you might have got some classes for today extension,

You can use App Group with User Defaults functionality to transfer data from iOS app to today Extension classes

1) To set data you can use

var defaults: NSUserDefaults = NSUserDefaults(suiteName: <GroupID>)!
defaults.setObject(<YourArray, forKey:<KeyName>)

2) In Today Extension target's Class you can fetch that array like this

var defaults: NSUserDefaults = NSUserDefaults(suiteName: <GroupID>)!
var <YourArray> = defaults.stringForKey(<KeyName>)

Note : This is just pseudo code might contain error

Upvotes: 2

Related Questions