Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Cannot assign through subscript: subscript is get only

Tags:

swift

I am fairly new to the Swift syntax and am receiving this error with my code "Cannot assign through subscript: subscript is get only"

This is from the line: friendDictionary[(friendUID as? String)!] = ["name": friendsData!["name"]]

Any advice on the correct way of doing it would be very helpful.

  func getFriendsUIDs() {

        if FBSDKAccessToken.currentAccessToken() == nil {

            print("failed to start graph request")
            return

        }else{
        }


        if FBSDKAccessToken.currentAccessToken() != nil {
        }

let parameters = ["fields": "name, id, picture"]
FBSDKGraphRequest(graphPath: "/me/friends", parameters: parameters).startWithCompletionHandler {
(NSURLConnection, result, requestError) in
let friendIds = result["id"] as? NSDictionary
let friendsData = friendIds!["data"] as? [NSDictionary]

        var ref: FIRDatabaseReference!
        ref = FIRDatabase.database().reference()
        ref.child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("friendUIDs").observeEventType(.Value, withBlock: { (snapshot) in
            self.FriendUIDs = NSArray()
            self.FriendUIDs = (snapshot.value as? NSArray)!
            print(self.FriendUIDs)
            var friendDictionary = NSDictionary()
            for friendUID in self.FriendUIDs {
                friendDictionary[(friendUID as? String)!]  = ["name": friendsData!["name"]]
            }

            self.fetchFriendFeed(friendDictionary)
        }) { (error) in
            print(error.localizedDescription)
        }
}

}



func fetchFriendFeed(friendDictionary: NSDictionary) {


var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()

for friendUID in FriendUIDs {

    ref.child("users").child(friendUID as! String).child("Agenda").observeEventType(.ChildAdded, withBlock: { (snapshot) in

        print(snapshot)
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let friendPost = FriendPost()
            friendPost.picture = friendDictionary[friendUID as! String]? ["picture"] as? String
            friendPost.activity = dictionary["activity"] as? String
            friendPost.date = dictionary["date"] as? String
            friendPost.time = dictionary["time"] as? String
            friendPost.friendname = friendDictionary[friendUID as! String]?  ["name"] as? String
            self.friendPosts.append(friendPost)

            dispatch_async(dispatch_get_main_queue(), {
                self.collectionView?.reloadData()
like image 427
Darren Avatar asked Feb 06 '17 23:02

Darren


2 Answers

Nothing to do with Swift. You've elected to use Objective-C, in effect, by making friendDictionary an NSDictionary. NSDictionary is immutable; you can't assign into it or alter it in any way. That is simply a fact about Objective-C. The Swift var declaration makes no difference to this fact.

A better choice, since you are writing in Swift, would be to use a Swift dictionary, which is [AnyHashable:Any]() (in Swift 3). This will interchange with NSDictionary when you are talking to Objective-C, but it will give you a mutable dictionary because you (rightly) declared it with var.

like image 140
matt Avatar answered Sep 18 '22 08:09

matt


Have you tried using NSMutableDictionary? That solved the issue for me.

like image 25
RossM Avatar answered Sep 19 '22 08:09

RossM