0
let newFeed = Feed(profilePicture: "image_boyce_avenue",artistName: "Boyce Avenue", videoUrl: "boyce_avenue-dont_let_me_down",videoTitle:"Dont let me down", videoViews: "160",likes: "200",shoutouts: "200",comments: [Comment.init(userName: "Megan Nicole", userComment: "Wow!", likes: "50", timeOfComment: Date())],votes: "50", dateCreated: Date(), feedType: FeedContentType.VIDEO_FEED.rawValue, userActivity :"This user liked your video")
ArtistProfileData.sharedInstance.add(array: newFeed,artistName: name)
print("Count in dictionary ",ArtistProfileData.sharedInstance.artistProfileDict[name]?.feeds.count as Any)
在上面的代碼中,數組數在追加後返回正確。在單個類中追加一個數組並在另一個控制器中返回0計數
當我試圖訪問同一陣列中的另一個類的計數返回爲0
print("Count in anotherbview",ArtistProfileData.sharedInstance.artistProfileDict[artistSelected]?.feeds.count as Any)
return (ArtistProfileData.sharedInstance.artistProfileDict[artistSelected]?.feeds.count)!
以下是Singleton類
class ArtistProfileData{
static let sharedInstance = ArtistProfileData()
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) {
ArtistProfileData.sharedInstance.artistProfileDict[artistName]!.feeds.append(array)
}
}
以下是配置文件對象
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
}
}
我無法弄清楚這裏的問題。任何幫助將不勝感激。謝謝。
您的'Profile'對象的外觀如何? – ingaham
我編輯了問題@ingaham –
不幸的是,這個錯誤與這段代碼可能沒有關係......我只是複製了代碼(也創建了Feed結構),並且它按預期工作。然而,你的解決方案是修改'feeds'數組,很容易,所以可能在某個時候你重載它或從中刪除對象 – ingaham