Ios 通过updateApplicationContext将字典从手表发送到iPhone

Ios 通过updateApplicationContext将字典从手表发送到iPhone,ios,watchos-2,watchconnectivity,Ios,Watchos 2,Watchconnectivity,我的手表连接有问题 在我的应用程序中,我向我的AppleWatch发送一个包含数据的字典。在我的手表上一切正常。在那里,用户可以更改一些数据(我有一个艺术家列表,用户可以在其中选中或取消选中艺术家)。更改后,列表应通过WatchConnectivity发送回我的iPhone。我使用函数updateApplicationContext。在我的AppDelegate类中,字典被正确读取,但在此之后,我想将我的美工设置为NSUserDefaults,但如果我进行调试,NSUserDefaults中没有

我的手表连接有问题

在我的应用程序中,我向我的AppleWatch发送一个包含数据的字典。在我的手表上一切正常。在那里,用户可以更改一些数据(我有一个艺术家列表,用户可以在其中选中或取消选中艺术家)。更改后,列表应通过WatchConnectivity发送回我的iPhone。我使用函数updateApplicationContext。在我的AppDelegate类中,字典被正确读取,但在此之后,我想将我的美工设置为NSUserDefaults,但如果我进行调试,NSUserDefaults中没有更改的值

任何人都知道如何将数组等复杂对象发送回手机

这是我的代理

class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?


func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){


    if(applicationContext["myUserProfile"] != nil){
        Utils().importMyUserProfileFromSession(applicationContext, getCompletionHandler: {(artists) -> Void in

            Utils().saveArtists(artists, key: "profileArtistsFromWatch")
            //in artists  the Values are correct, so i save them here
            //but if i want to read them out(load and save Function works!) the Array is empty...
            let artists1 = Utils().loadArtists("profileArtistsFromWatch")
            })   
    }
}
}

这是我的函数importMyUserProfileFromSession

  func importMyUserProfileFromSession(applicationContext: [String: AnyObject], getCompletionHandler : (artists: [Artist]) -> Void){
    let me = self.getProfileData()
    var myArtists = me.artists


    if(applicationContext["myUserProfile"] != nil){
        let dictUser :[String: AnyObject] = applicationContext["myUserProfile"] as! [String: AnyObject]
        // print(dictUser)
        var artists : [Artist] = [Artist]()
        if dictUser["artists"] != nil{
            let dictArtists :[Int: AnyObject] = dictUser["artists"] as! [Int: AnyObject]
            for j in 0..<dictArtists.count{
                let dictArtist :[String: AnyObject] = dictArtists[j] as! [String: AnyObject]
                var a : Artist = Artist()
                if dictArtist["id"] != nil{
                    let indexOfArtist = self.getArtistById(dictArtist["id"] as! Int, artists: myArtists)
                    if(indexOfArtist != -1){
                        a.id = myArtists[indexOfArtist].id as! Int
                        print(a.id)
                    }
                    a.idInApp = dictArtist["id"] as! Int
                }
                if dictArtist["name"] != nil{
                    a.name = dictArtist["name"] as! String
                }
                if dictArtist["image"] != nil{
                    let imageData: NSData = dictArtist["image"] as! NSData
                    a.imageData = imageData

                }
                if dictArtist["active"] != nil{
                    let active: Bool = dictArtist["active"] as! Bool
                    a.display = active

                }

                artists.append(a)
            }
            getCompletionHandler(artists: artists)   
        }
    }

}
func importMyUserProfileFromSession(applicationContext:[String:AnyObject],getCompletionHandler:[Artisters:[Artister])->Void){
让我=self.getProfileData()
var myArtists=me.artists
如果(applicationContext[“myUserProfile”]!=nil){
让dictUser:[String:AnyObject]=applicationContext[“myUserProfile”]作为![String:AnyObject]
//打印(用户)
var艺术家:[艺术家]=[艺术家]()
如果用户[“艺术家”]!=nil{
让dictArtists:[Int:AnyObject]=dictUser[“artists”]as![Int:AnyObject]

对于0中的j。如果
artists
持有新值,则问题可能出现在
saveArtists()
loadArtists()
中。您应该知道
standardUserDefaults
对于watch和phone是不同的?要使其生效,您需要在watch分机和手机上启用应用程序组,然后使用
[[NSUserDefaults alloc]initWithSuiteName:APP\u GROUP\u ID]
而不仅仅是
[NSUserDefaults standardUserDefaults]
I trink我发现了错误。我保存了我的Artistids,我从facebook上获得了Integer。我认为把它们保存为Integer太大了,所以我将它们保存为字符串,我trink现在它可以工作了…愚蠢的问题…但感谢你的回答。WatchOS 2中不推荐使用手表组,我昨天尝试过,但它不再工作了。。。