I am trying to receive number of properties of Now Playing item.
Per Apple documentation:
Anytime the app accesses more than one property, enumerating over a set of property keys is more efficient than fetching each individual property.
So I tried to use their method:
func enumerateValues(forProperties properties: Set<String>,
using block: #escaping (String, Any, UnsafeMutablePointer<ObjCBool>) -> Void)
But I just cannot understand how it should be used.
My code:
//MARK: Properties
var allProperties: [String: Any]
var albumTitle: String?
var albumArtist: String?
var title: String?
var artist: String?
var artwork: UIImage?
var genre: String?
var lyrics: String?
var releaseDate: Date?
var playbackDuration: TimeInterval?
var rating: Int?
var assetURL: URL?
var isExplicitItem: Bool?
var isCloudItem: Bool?
var hasProtectedAsset: Bool?
let propertiesSet: Set<String> = [MPMediaItemPropertyAlbumTitle,
MPMediaItemPropertyAlbumArtist,
MPMediaItemPropertyTitle,
MPMediaItemPropertyArtist,
MPMediaItemPropertyArtwork,
MPMediaItemPropertyGenre,
MPMediaItemPropertyLyrics,
MPMediaItemPropertyReleaseDate,
MPMediaItemPropertyPlaybackDuration,
MPMediaItemPropertyRating,
MPMediaItemPropertyAssetURL,
MPMediaItemPropertyIsExplicit,
MPMediaItemPropertyIsCloudItem,
MPMediaItemPropertyHasProtectedAsset]
func getAllMetadata() {
allProperties = nowPlaying?.enumerateValues(forProperties: propertiesSet,
using: //No idea what to put here
-> [String: Any])
}
How to use it properly?
Finally I figured out how to use it. So I re-wrote my function as follows:
func getAllMetadata() {
var allProperties: [String: Any] = [:]
nowPlaying?.enumerateValues(forProperties: propertiesSet, using: {key,value,_ in allProperties[key] = value})
albumTitle = allProperties["albumTitle"] as? String
//and so on
}
This documentation helps me to understand the proper usage -
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html
Related
I have the following class
// LearningItem
class LearningItem : NSObject {
var id: String
var title: String
var subtitle: String?
var image: String
var uploadDate: Int
init(id: String, title: String, image: String, uploadDate: Int) {
self.id = id
self.title = title
self.image = image
self.uploadDate = uploadDate
}
I have another class
// Book.swift
class Book: LearningItem {
var publishDate: String?
var author: String?
var mediaUrl: String?
var video : String?
var tags: [String]?
var lists: [String: AnyObject]?
var readCount: Int
var categories: [String]?
var courses: [String]?
var amazonBuyUrl: String?
var starCount: Int
var read: Bool?
var completed = [String : Int]()
var stars = [String : Int]()
var completedDate : Int?
var desc: String
var epub: String
var authorDesc: String
init(id: String, title: String, desc: String, authorDesc: String, image: String, epub: String, readCount: Int, uploadDate: Int, starCount: Int) {
super.init(id: id, title: title, image: image, uploadDate: uploadDate)
self.id = id
self.desc = desc
self.authorDesc = authorDesc
self.title = title
self.epub = epub
self.image = image
self.readCount = readCount
self.uploadDate = uploadDate
self.starCount = starCount
}
I get the error "Property 'self.readCount' not initialized at super.init call"
where I call "super.init(id: id, title: title, image: image, uploadDate: uploadDate)" in Book.swift
Class initialisation not finished until it's designated initializers
not finished with initializing all properties
and after that you can call super class's designated initializers
Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.
SO
Class initialization in Swift is a two-phase process. In the first phase, each stored property is assigned an initial value by the class that introduced it. Once the initial state for every stored property has been determined, the second phase begins, and each class is given the opportunity to customize its stored properties further before the new instance is considered ready for use.
apple docs https://docs.swift.org/swift-book/LanguageGuide/Initialization.html
so
class Book: LearningItem {
var publishDate: String?
var author: String?
var mediaUrl: String?
var video : String?
var tags: [String]?
var lists: [String: AnyObject]?
var readCount: Int
var categories: [String]?
var courses: [String]?
var amazonBuyUrl: String?
var starCount: Int
var read: Bool?
var completed = [String : Int]()
var stars = [String : Int]()
var completedDate : Int?
var desc: String
var epub: String
var authorDesc: String
init(id: String, title: String, desc: String, authorDesc: String, image: String, epub: String, readCount: Int, uploadDate: Int, starCount: Int) {
self.readCount = readCount
self.starCount = starCount
self.desc = desc
self.epub = epub
self.authorDesc = authorDesc
super.init(id: id, title: title, image: image, uploadDate: uploadDate)
}
}
Having the same structure of data models I want to merge data so I can post values to server on button click.
Unit Model : I pass this model to AcceptanceViewcontroller in var UnitData for entering final
remarks.
Acceptance Model: Only for Final remarks (comments) and
few values.
How to merge both data into one swift.
var AppData : Acceptance?
var UnitData: Unit?
let decoder = JSONDecoder()
let response = try decoder.decode(Acceptance.self, from: result! as! Data)
self.AppData = response
self.tableView.reloadData()
Data Models:
struct Unit : Codable {
var sectionList : [SectionList]?
}
struct Acceptance : Codable {
var sectionList : [SectionList]?
}
struct SectionList : Codable {
let title : String?
var items : [Item]?
}
struct Item : Codable {
let actionType : Int?
let actionUrl : String?
let bgColor : String?
let booleanValue : Bool?
var textField : String?
var textValue : String?
let unitId : Int?
let latitude : Double?
let longitude : Double?
let actionParamData: String?
let actionTitle: String?
let pickList: [SectionList]?
let multiSelect: Bool?
let selectedValue: [String]?
let version: Int?
let masterId: Int?
let itemValue: String?
}
Please, remember to name your variables starting with lower cases. AppData and UnitData should be renamed to appData and unitData respectively.
Anyway, the solution provided by #AlexandrKolesnik in the comments works. You should do:
unitData?.sectionList?.append(contentsOf: appData?.sectionList ?? [])
If you don't want unitData to be modified by adding appData to it, just make another array and append unitData and then appData.
var allSectionList: [SectionList] = []
allSectionList.append(contentsOf: unit?.sectionList ?? [])
allSectionList.append(contentsOf: acceptance?.sectionList ?? [])
To add the behavior described in the comments of this answer, I'd add a boolean variable to SectionList that indicates if it has been modified. And then whatever you want to merge both lists, only append those SectionList that have said variable as true. For example:
struct SectionList : Codable {
let title : String?
var items : [Item]?
var modified = false
}
If your user modifies that SectionList, then set the modified variable as true. In the moment of merging the data:
var allSectionList: [SectionList] = []
allSectionList.append(contentsOf: unit?.sectionList ?? [])
acceptance?.sectionList?.forEach { section in
if section.modified {
allSectionList.append(section)
}
}
You might need to set the modified variable to false once you've added it to the merged list, but that depends on you and if you need it.
how one can fetch a JSON file in the following format:
https://developers.themoviedb.org/3/people/get-popular-people
Alamofire is used to fetch the data from online database.
I don't know exactly how to format the JSON file received so the nested array can have the elements saved in an instance MovieModel(poster: UIImage,
name: String,
rating: Double,
year: String,
posterLink: String,
id: Int)
ERROR: Fatal error: NSArray element failed to match the Swift Array Element type
My code looks like:
class MovieModel {
private var _poster: UIImage!
private var _background: UIImage!
private var _name: String! // original_title
private var _overview: String! // overview
private var _rating: Double! // vote_average
private var _year: String!
private var _posterLink: String!
private var _backgroundLink: String!
private var _genres: [Int]!
private var _id: Int!
init(poster: UIImage,
name: String,
rating: Double,
year: String,
posterLink: String,
id: Int){
_poster = poster
_name = name
_rating = rating
_year = year
_posterLink = posterLink
_id = id
getPosterImage()
}
}
class ActorModel {
private var _poster : UIImage!
private var _birthday : String?
private var _known_for_department : String?
private var _deathday : String?
private var _id : Int!
private var _known_for : [MovieModel]?
private var _name : String!
private var _also_known_as : [String]!
private var _gender : Int!
private var _biography : String?
private var _popularity : Double?
private var _place_of_birth : String?
private var _profile_path : String?
private var _adult : Bool!
private var _imdb_id : String!
private var _homepage : String!
init(poster : UIImage, id : Int, known_for: [MovieModel], name: String, popularity : Double, profile_path : String) {
_poster = poster
_id = id
_known_for = known_for
_name = name
_popularity = popularity
_profile_path = profile_path
getPosterImage()
}
}
func getPopularActors(){
let url = BASE_URL + "/person/popular?" + API_KEY + LANG + "&page=1"
self.actorList = []
Alamofire.request(url).responseJSON {response in
if let result = response.result.value as? Dictionary<String,Any> {
if let list = result["results"] as? [Dictionary<String,Any>] {
for i in list {
self.actorList.append(ActorModel(
poster: UIImage(),
id: i["id"] as! Int,
known_for: i["known_for"] as! [MovieModel],
name: i["name"] as! String,
popularity: i["popularity"] as! Double,
profile_path: "\(self.IMAGE_URL)\(i["profile_path"] as! String)"
))
}
if let del = self.actorDelegate {
del.transferActors(data: self.actorList)
}
}
}
}
}
You should really use Decodable for this. But building on your current solution:
for i in list {
var movieModels = [MovieModel]()
if let knownForArray = i["_known_for"] as? [[String: Any]] {
for knownFor in knownForArray {
movieModels.append(MovieModel(
poster: UIImage(),
name: knownFor["name"] as! String,
rating: knownFor["rating"] as! Double,
... //And so on
))
}
}
self.actorList.append(ActorModel(
poster: UIImage(),
id: i["id"] as! Int,
known_for: movieModels,
name: i["name"] as! String,
popularity: i["popularity"] as! Double,
profile_path: "\(self.IMAGE_URL)\(i["profile_path"] as! String)"
))
}
I'm trying to use JSONDecoder to convert a JSON to Structs in Swift, so I wrote all the Structs, revised them for hours, and it still gives me this error. I don't know if there is a way to see the line that gives this.
I'll post my struct below and the Json File links right after.
The complete error description is:
typeMismatch(Swift.Dictionary<Swift.String, Any>,
Swift.DecodingError.Context(codingPath: [], debugDescription:
"Expected to decode Dictionary<String, Any> but found an array
instead.", underlyingError: nil))
// Created by Breno Ramos on 28/12/17.
// Copyright © 2017 brenor2. All rights reserved.
//
import Foundation
struct Owner:Decodable {
let login : String?
let id : Double?
let avatar_url : String?
let gravatar_id : String?
let url : String?
let html_url : String?
let followers_url : String?
let following_url : String?
let gists_url : String?
let starred_url : String?
let subscriptions_url : String?
let organizations_url : String?
let repos_url : String?
let events_url : String?
let received_events_url : String?
let type : String?
let site_admin : Bool?
}
struct License:Decodable {
let key : String?
let name : String?
let spdx_id : String?
let url : String?
}
struct Repo:Decodable {
let id : Double?
let name : String?
let full_name : String?
let owner : Owner?
let `private` : Bool?
let html_url : String?
let description : String?
let fork : Bool?
let url : String?
let forks_url : String?
let keys_url : String?
let collaborators_url : String?
let teams_url : String?
let hooks_url : String?
let issue_events_url : String?
let events_url : String?
let assignees_url : String?
let branches_url : String?
let tags_url : String?
let blobs_url : String?
let git_tags_url : String?
let git_refs_url : String?
let trees_url : String?
let statuses_url : String?
let languages_url : String?
let stargazers_url : String?
let contributors_url : String?
let subscribers_url : String?
let subscription_url : String?
let commits_url : String?
let git_commits_url : String?
let comments_url : String?
let issue_comment_url : String?
let contents_url : String?
let compare_url : String?
let merges_url : String?
let archive_url : String?
let downloads_url : String?
let issues_url : String?
let pulls_url : String?
let milestones_url : String?
let notifications_url : String?
let labels_url : String?
let releases_url : String?
let deployments_url : String?
let created_at : String?
let updated_at : String?
let pushed_at : String?
let git_url : String?
let ssh_url : String?
let clone_url : String?
let svn_url : String?
let homepage : String?
let size : Double?
let stargazers_count : Double?
let watchers_count : Double?
let language : String?
let has_issues : Bool?
let has_projects : Bool?
let has_downloads : Bool?
let has_wiki : Bool?
let has_pages : Bool?
let forks_count : Double?
let mirror_url : String?
let archived : Bool?
let open_issues_count : Double?
let license : License?
let forks : Double?
let open_issues : Double?
let topics : Topic?
let permissions : Permissions?
let watchers : Double?
let default_branch : String?
// let score : Double?
// let subscribers_count : Double?
// let network_count : Double?
// let allow_rebase_merge: Bool?
// let allow_squash_merge: Bool?
// let allow_merge_commit: Bool?
}
struct Topic:Decodable {
let topics : [String]?
}
struct Permissions:Decodable {
let admin : Bool
let push : Bool
let pull : Bool
}
struct RepoList:Decodable{
let total_count : Int?
let incomplete_results : Bool?
let items : [Repo]?
}
struct User:Decodable {
let login: String?
let id: Double?
let avatar_url: String?
let gravatar_id: String?
let url: String?
let html_url: String?
let followers_url: String?
let following_url: String?
let gists_url: String?
let starred_url: String?
let subscriptions_url: String?
let organizations_url: String?
let repos_url: String?
let events_url: String?
let received_events_url: String?
let type: String?
let site_admin: Bool?
}
struct Creator:Decodable {
let login: String?
let id: Double?
let avatar_url: String?
let gravatar_id: String?
let url: String?
let html_url: String?
let followers_url: String?
let following_url: String?
let gists_url: String?
let starred_url: String?
let subscriptions_url: String?
let organizations_url: String?
let repos_url: String?
let events_url: String?
let received_events_url: String?
let type: String?
let site_admin: Bool?
}
struct Link:Decodable {
let href :String?
}
struct _Links:Decodable {
let `self` :Link?
let html :Link?
let issue :Link?
let comments :Link?
let review_comments :Link?
let review_comment :Link?
let commits :Link?
let statuses :Link?
}
struct Base:Decodable {
let label :String?
let ref :String?
let sha :String?
let user :User?
let repo :Repo?
}
struct Head:Decodable {
let label :String?
let ref :String?
let sha :String?
let user :User?
let repo :Repo?
}
struct Milestone:Decodable {
let url:String?
let html_url:String?
let labels_url:String?
let id: Double?
let number:Double?
let title:String?
let description:String?
let creator:Creator?
let open_issues:Double?
let closed_issues:Double?
let state:String?
let created_at:String?
let updated_at:String?
let closed_at:String?
let due_on:String?
}
struct Assignee:Decodable {
let login :String?
let id :Double?
let avatar_url :String?
let gravatar_id :String?
let url :String?
let html_url :String?
let followers_url :String?
let following_url :String?
let gists_url :String?
let starred_url :String?
let subscriptions_url :String?
let organizations_url :String?
let repos_url :String?
let events_url :String?
let received_events_url :String?
let type :String?
let site_admin :Bool?
}
struct Reviewers:Decodable {
let login: String?
let id: Double?
let avatar_url: String?
let gravatar_id: String?
let url: String?
let html_url: String?
let followers_url: String?
let following_url: String?
let gists_url: String?
let starred_url: String?
let subscriptions_url: String?
let organizations_url: String?
let repos_url: String?
let events_url: String?
let received_events_url: String?
let type: String?
let site_admin: Bool?
}
struct Pull:Decodable {
let id: Double?
let url:String?
let html_url:String?
let diff_url:String?
let patch_url:String?
let issue_url:String?
let number:Double?
let state:String?
let locked:Bool?
let title:String?
let user:User?
let body:String?
let created_at:String?
let updated_at:String?
let closed_at:String?
let merged_at:String?
let merge_commit_sha: String?
let assignee: Assignee?
let assignees: [Assignee]?
let requested_reviewers: [Reviewers]?
let milestone:Milestone?
let commits_url:String?
let review_comments_url:String?
let review_comment_url:String?
let comments_url:String?
let statuses_url:String?
let head:Head?
let base:Base?
let _links:_Links?
let author_association:String?
}
struct PullList:Decodable {
let pulls:[Pull]?
}
/////////////////////////////////////////////////////////
1.This one is working fine with this structs:
2.This one is the one that gives the typeMismatch error
You're probably doing this right now:
let decoder = JSONDecoder()
let repoList = decoder.decode(RepoList.self, from: data)
which is fine for the response with a top-level Object.
To decode JSON responses that are top-level Arrays, use code like this instead:
let decoder = JSONDecoder()
let repos = decoder.decode([Repo].self, from: data)
If you have a problematic JSON, which can contain number or string for some keys, you can decode object without that property and manually set that property after decoding.
For example, I have a Vehicle class inside HistoryItem. In Vehicle model_year can be empty String or non empty Int. Here I am decoding modelYear manually using NSDictionary and trying to get Int. Swift 4 cannot do it automatically.
do {
// Decoding HistoryItem from JSON
let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
let decoder = JSONDecoder()
let historyItem = try decoder.decode(HistoryItem.self, from: jsonData)
if let modelYear = (dict as NSDictionary).value(forKeyPath: "vehicle.model_year") as? Int {
historyItem.vehicle?.modelYear = modelYear
}
// Saving HistoryItem to Realm
let realm = try! Realm()
try! realm.write {
realm.add(historyItem, update: true)
}
} catch {
print(error.localizedDescription)
}
This is my Vehicle class that is contained inside HistoryItem:
class Vehicle: Object, Codable {
#objc dynamic var VIN: String = ""
#objc dynamic var make: String?
#objc dynamic var modelName: String?
#objc dynamic var recallCount: Int = 0
#objc dynamic var modelYear: Int = 0
override static func primaryKey() -> String? {
return "VIN"
}
private enum CodingKeys: String, CodingKey {
case VIN = "vin"
case make
case modelName = "model_name"
case recallCount = "recall_count"
}
}
As you see, there is no model_year key in CodingKeys.
This function gets variables from another file users.swift
append data
func getData(salval:String )
{ self.tabArray.removeAll()
ref = Database.database().reference()
let userID = Auth.auth().currentUser!.uid
if salval != "" {
ref?.child("Schedule").child("Codaphic").child(salval).child(userID).observe(.childAdded, with: { (snapshot)
in
if let dictionary = snapshot.value as? [String : AnyObject]
{
let user = users()
user.setValuesForKeys(dictionary)
self.tabArray.append(user)
self.tableView1.reloadData()
}
})
}
}
This is users.swift
import UIKit
class users : NSObject
{
var cname : String?
var _logi: String?
var key : String?
var wname : String?
var address : String?
var endtime : String?
var _leti : String?
var starttime : String?
var date : String?
var groupname : String?
var month : String?
}
this file is inherited with NSObject
but when project debugging its show error
If you're using user.setValuesForKeys(dictionary) it means that the variables in your User class MUST match the same names as the ones in your database. The safest way is to remove user.setValuesForKeys(dictionary) and use:
let user = User()
user.cname = dictionary["cname"] as? String
user.date = dictionary["data"] as? String
etc...