无法将数组附加到具有空数组的字典中

时间:2017-03-24 10:41:22

标签: ios arrays swift dictionary singleton

我有一个Profile Data单例类,如下所示。

我正在尝试将数据存储到字典中的空数组中。

将数据附加到数组后,数组的计数也为0.

        class ProfileData{

            static let sharedInstance = ProfileData()

             var artistProfileDict = [String : Profile]()
             var loggedInUserProfile = Profile(artistName: "John Smith", artistDescription: "Admiral of New England, English soldier, explorer, and author.", totalLikes: "174", totalViews: "200", totalFollowing: "100",totalFollowers:"50",imageUrl:"image_singer", feeds:[] )


            private init() {
                getProfilesDictionary()
            }

             func getProfilesDictionary()->[String: Profile]{
                artistProfileDict["John Smith"] = loggedInUserProfile
                return artistProfileDict
            }

            func add(array: Feed, artistName: String) {

               artistProfileDict[artistName]!.feeds.append(array)

            }

        }

在另一个视图中,我试图将数组添加到字典中的空数组,如下所示

    let newFeed = Feed(profilePicture: "image",artistName: "New", 
    videoUrl: "url",videoTitle:"New", videoViews: "160",likes: 
    "200",shoutouts: "200",comments: [],votes: "50", dateCreated: Date(),
     userActivity :"This user liked your video")


ProfileData.sharedInstance.add(array: newFeed,artistName:"John Smith")

将数组附加到字典中的空数组后,我仍然将数组的计数为0。

我无法在这里找出问题所在。任何帮助将不胜感激。谢谢。

个人资料类

struct Profile {
    var artistName: String
    var artistDescription: String
    var totalLikes: String
    var totalViews: String
    var totalFollowing: String
    var totalFollowers: String
    var imageUrl: String
    var feeds : [Feed]

    init(artistName: String,artistDescription:String,totalLikes:String,totalViews:String,totalFollowing:String,totalFollowers:String,imageUrl:String, feeds:[Feed]) {

        self.artistName = artistName
        self.artistDescription = artistDescription
        self.totalLikes = totalLikes
        self.totalViews = totalViews
        self.totalFollowing = totalFollowing
        self.totalFollowers = totalFollowers
        self.imageUrl = imageUrl
        self.feeds = feeds
    }
}

1 个答案:

答案 0 :(得分:0)

工作正常

ProfileData.sharedInstance.add(array: newFeed,artistName:"John Smith")

ProfileData.sharedInstance.artistProfileDict["John Smith"]?.feeds.count // 1

可能您使用了错误的班级ArtistProfileData而不是ProfileData