I have a realm database. So that the database is more readable. I have designed the questionbank as follows:
class Question: Object {
#objc dynamic var id: Int = 0
#objc dynamic var name: String = ""
#objc dynamic var answered: Bool = false
#objc dynamic var lastAnswer: Bool = false
#objc dynamic var howManyTimesAnswered: Int = 0
#objc dynamic var answer0: String = ""
#objc dynamic var answer1: String = ""
#objc dynamic var answer2: String = ""
#objc dynamic var answer3: String = ""
#objc dynamic var correctAnswer: Int = 0
let parentCategory = LinkingObjects(fromType: Category.self, property: "questions") //back relationship to category
}
I am trying to pull out the answers and putting them into an array of tuples (String, Bool)
var currentAnswers = [(String, Bool)]()
for i in 0...3 {
if i == question.correctAnswer {
currentAnswers.append(("question.answer2", true))
} else {
currentAnswers.append((question.answer1, false))
}
}
what I want to achieve is something like this where the answer pulled out is equal to i obviously the one below will not compile
for i in 0...3 {
if i == question.correctAnswer {
currentAnswers.append((question.answer(i), true))
} else {
currentAnswers.append((question.answer(i), false))
}
}
You can use Key path Try this code
var currentAnswers = [(String, Bool)]()
for i in 0...3 {
if i == question.correctAnswer {
currentAnswers.append((question.value(forKey: "answer\(i)") as! String, true))
} else {
currentAnswers.append((question.value(forKey: "answer\(i)") as! String, false))
}
}
I have managed to get a workaround it. Only one line of extra code.
I hope this will help if someone wants to achieve something similar.
var currentAnswers = [(String, Bool)]()
let answers = [question.answer0, question.answer1, question.answer2, question.answer3]
for i in 0...3 {
if i == question.correctAnswer {
currentAnswers.append((answers[i], true))
} else {
currentAnswers.append((answers[i], false))
}
}
I have been using tuple above for true randomization so that the answers always show up in different order on the answer buttons.
Related
I am using xcode playgrounds toying around with some code and I get this error Use of unresolved identifier 'colorArray' when I try to call a function. How do I fix this I know it has to be simple.
import Foundation
class Solution{
var colorArray = ["Red","blue","green","black","red","blue","yellow","purple","Red","Red","Red","yellow","yellow","black","green","black","purple","black","yellow","purple","purple","blue","yellow","blue","green","green","yellow","pink"]
func getMostCommonColor(array: [String]) -> [String] {
var topColors: [String] = []
var colorDictionary: [String: Int] = [:]
for color in array {
if let count = colorDictionary[color] {
colorDictionary[color] = count + 1
}else{
colorDictionary[color] = 1
}
}
let highestValue = colorDictionary.values.max()
for (color,count) in colorDictionary {
if colorDictionary[color] == highestValue {
topColors.append(color)
}
}
return topColors
}
}
let solution = Solution()
solution.getMostCommonColor(array: colorArray) //This is where I get the error
The way you've defined it, colorArray is an instance property of Solution, meaning you need to access it through some instance of the Solution class.
You've already created an instance down below, so you could do this:
let solution = Solution()
solution.getMostCommonColor(array: solution.colorArray)
However, I suspect you meant for colorArray to be a global variable of sorts (since this is just a playground, after all), so another option is to move colorArray out of the Solution class:
var colorArray = ["Red","blue","green","black","red","blue","yellow","purple","Red","Red","Red","yellow","yellow","black","green","black","purple","black","yellow","purple","purple","blue","yellow","blue","green","green","yellow","pink"]
class Solution {
func getMostCommonColor(array: [String]) -> [String] {
var topColors: [String] = []
var colorDictionary: [String: Int] = [:]
for color in array {
if let count = colorDictionary[color] {
colorDictionary[color] = count + 1
}else{
colorDictionary[color] = 1
}
}
let highestValue = colorDictionary.values.max()
for (color,count) in colorDictionary {
if colorDictionary[color] == highestValue {
topColors.append(color)
}
}
return topColors
}
}
let solution = Solution()
solution.getMostCommonColor(array: colorArray)
I am using RealmSwift.
Data is a one-to-many relationship.
I'm having trouble because I don't know how to sort the list in RealmSwift.
I want to sort the tasks linked to the TaskList.
Thank you.
class TaskList: Object, Identifiable {
#objc dynamic var id = NSUUID().uuidString
#objc dynamic var title = ""
#objc dynamic var createdAt = NSDate()
var tasks: List<Task> = List<Task>()
override static func primaryKey() -> String? {
return "id"
} }
class Task: Object, Identifiable {
#objc dynamic var id = NSUUID().uuidString
#objc dynamic var title = ""
#objc dynamic var createdAt = NSDate()
private let lists = LinkingObjects(fromType: TaskList.self, property: "tasks")
var list: TaskList { return list.first! }
override static func primaryKey() -> String? {
return "id"
} }
If you want your tasks stored in an ordered fashion you'll have to manually do an ordered insert.
extension List {
func insert<V: Comparable>(_ object: Element, orderedBy keyPath: KeyPath<Element, V>) {
var index = 0
for i in 0..<count {
if self[i][keyPath: keyPath] >= object[keyPath: keyPath] {
break
}
index = i + 1
}
insert(object, at: index)
}
}
let list = TaskList()
let tasks = [
Task(title: "J"),
Task(title: "Z"),
Task(title: "T"),
Task(title: "J"),
Task(title: "Z"),
]
tasks.forEach {
list.tasks.insert($0, orderedBy: \.title)
}
However, I find it much easier to keep Lists unsorted and retrieve sorted Results whenever I need to display the data. To sort by a single property just call sorted(byKeyPath:):
let sortedTasks = taskList.tasks.sorted(byKeyPath: "title")
To sort by multiple fields call sorted(by:):
let sortedTasks = taskList.tasks.sorted(by: [
SortDescriptor(keyPath: "title"),
SortDescriptor(keyPath: "createdAt")
])
Alternatively, you can add a sorted property to your Model:
class TaskList: Object, Identifiable {
#objc dynamic var id = UUID().uuidString
#objc dynamic var title = ""
#objc dynamic var createdAt = Date()
var tasks = List<Task>()
lazy var sortedTasks: Results<Task> = tasks.sorted(byKeyPath: "title")
override class func ignoredProperties() -> [String] {
return ["sortedTasks"]
}
override static func primaryKey() -> String? {
return "id"
}
}
You cannot get a LinkingObjects itself to be sorted, but you can call LinkingObjects.sorted(byKeyPath:), which returns a Results instance containing all elements of the LinkingObjects, just sorted.
class Task: Object, Identifiable {
#objc dynamic var id = NSUUID().uuidString
#objc dynamic var title = ""
#objc dynamic var createdAt = NSDate()
private let lists = LinkingObjects(fromType: TaskList.self, property: "tasks")
lazy var sortedLists = lists.sorted(byKeyPath: "createdAt")
var list: TaskList { return list.first! }
override static func primaryKey() -> String? {
return "id"
}
}
I think this is a one line answer. If you know which TaskList object you want to get the tasks for, and you want them ordered by say, creation date. This will do it
let taskResults = realm.objects(Task.self)
.filter("ANY lists == %#", taskList)
.sorted(byKeyPath: "createdAt")
i have two models (User and Project) as below:
class User: Object { // Users that logged in app in specific device atleast once
#objc dynamic var serverId: Int = 0
#objc dynamic var firstName: String = ""
#objc dynamic var lastName: String = ""
#objc dynamic var email: String?
#objc dynamic var company: String?
#objc dynamic var phoneNumber: String = ""
#objc dynamic var syncBaseTime: String = ""
let projects = LinkingObjects(fromType: Project.self, property: "user")
convenience init(_serverId: Int, _firstName: String, _lastName: String, _phoneNumber: String) {
self.init()
self.serverId = _serverId
self.firstName = _firstName
self.lastName = _lastName
self.phoneNumber = _phoneNumber
}
override static func primaryKey() -> String? {
return "serverId"
}
}
class Project: Object {
#objc dynamic var serverId: Int = 0
#objc dynamic var name: String = ""
#objc dynamic var stateId: Int = 0
#objc dynamic var stateName: String = ""
#objc dynamic var cityId: Int = 0
#objc dynamic var cityName: String = ""
#objc dynamic var user: User?
#objc dynamic var isOwner: Bool = false
#objc dynamic var isActive: Bool = false
#objc dynamic var compoundKey: String = ""
#objc dynamic var syncDetailTime: String = ""
let accountTitles = LinkingObjects(fromType: AccountTitle.self, property: "project")
let notes = LinkingObjects(fromType: Note.self, property: "project")
convenience init(_serverId: Int, _name: String, _stateId: Int, _stateName: String,
_cityId: Int, _cityName: String, _user: User, _isOwner: Bool, _isActive: Bool) {
self.init()
self.serverId = _serverId
self.name = _name
self.stateId = _stateId
self.stateName = _stateName
self.cityId = _cityId
self.cityName = _cityName
self.user = _user
self.isOwner = _isOwner
self.isActive = _isActive
self.compoundKey = "\(self.user!.serverId)-\(self.serverId)"
}
override static func primaryKey() -> String? {
return "compoundKey"
}
}
the problem occurs when I want to execute this query on them and get projects that current user working on them, but they are not currently choosen:
self.realm.objects(Project.self).filter(NSPredicate(format: "user.serverId == %# && isActive == true && serverId != %#", self.currentUser.serverId, self.currentProject.serverId))
I get this error with no more information from Xcode:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x11)
and I could not find where is my mistake and I will appreciate any help with this.
You are using the wrong specifier in the format string, %# is only for objects, and Int is not an object in terms of NSPredicate
Use %ld for Int
self.realm.objects(Project.self).filter(NSPredicate(format: "user.serverId == %ld && isActive == true && serverId != %ld", self.currentUser.serverId, self.currentProject.serverId))
Note: This is Swift. Please don't use ugly objective-c-ish variable names with starting underscore characters. self.name = name does compile.
I encounter a very strange bug, I have a PodEvent type array, and with a second function I get another array that I cast, when I make a print of the casted array with the result it shows me all the objects of the board...
PodEvent {
eventID = 2;
podID = 1;
drinkDate = 2018-09-25 10:00:00 +0000;
actualDrinkingTime = (null);
keyDate = 2018-09-25 13:00;
rawState = Forgotten;
}
But when I want to access the value of the object it returns to me as the default values! for example:
print(self.podEvents[1].eventID)
print(self.podEvents[1].podID)
outpout:
-1
-1
Here my class:
class PodEvent: Object {
#objc var eventID: Int = -1
#objc var podID: Int = -1
#objc var drinkDate: Date = Date()
#objc var actualDrinkingTime: Date? = Date()
var state: PodState = .future
#objc var keyDate: String = ""
#objc private var rawState: String!
convenience init(eventID: Int, podID: Int, drinkDate: Date, actualDrinkingTime: Date?, state: PodState){
self.init()
self.eventID = eventID
self.podID = podID
self.drinkDate = drinkDate
self.actualDrinkingTime = actualDrinkingTime
self.state = state
self.rawState = state.state
}
func setState(state: PodState){
self.state = state
rawState = state.state
}
override class func primaryKey() -> String? {
return "keyDate"
}
This bug is very strange
My code to fetch my array:
//Fetch pod history in internal database
self.databaseManager.fetch(object: PodEvent.self, predicate: nil, sortedBy: "keyDate", ascending: true) { success, results, error in
guard error == nil else {
Alerts.alertMessage(for: self, title: "ERROR".localized,
message: error!.localizedDescription,
closeHandler: nil)
return
}
self.podEvents = (results as! [PodEvent])
self.pods = pods
print(self.podEvents[1].eventID)
print(self.podEvents[1].podID)
}
and:
func fetch(object: Object.Type, predicate: NSPredicate?, sortedBy: String, ascending: Bool, completion: DatabaseCompletion?) {
do {
let realm = try Realm()
let objects: Results<Object>!
if let predicate = predicate {
objects = realm.objects(object).filter(predicate).sorted(byKeyPath: sortedBy, ascending: ascending)
} else {
objects = realm.objects(object).sorted(byKeyPath: sortedBy, ascending: ascending)
}
let objectsArray = Array(objects)
completion?(true, objectsArray, nil)
} catch let error {
print("Could not write object (type \(object)) to Realm:", error.localizedDescription)
completion?(false, nil, error)
}
}
im using realm
Your object definition is flawed, you need to add the dynamic keyword to all persisted properties.
class PodEvent: Object {
#objc dynamic var eventID = -1
#objc dynamic var podID = -1
#objc dynamic var drinkDate = Date()
#objc dynamic var actualDrinkingTime: Date? = Date()
var state: PodState = .future
#objc dynamic var keyDate = ""
#objc dynamic private var rawState: String!
...
}
In my application I want to implement separate class to keep all the temporary variables of Now Playing item for Music Player.
It has lots of properties with different types, but they should be handled in the same way. They should be handled in the class method "updateData" (see the end of code)
This is my code:
struct DataDefaults {
//MARK: Default properties
let albumTitle: String? = "Unknown Album"
let albumArtist: String? = "Unknown Artist"
let title: String? = "Unknown Title"
let artist: String? = "Unknown Artist"
let artwork: UIImage? = UIImage(named: "noartwork")!
let genre: String? = ""
let lyrics: String? = "No Lyrics"
let releaseDate: Date? = nil
let playbackDuration: TimeInterval? = 0
let rating: Int? = 0
let assetURL: URL? = nil
let isExplicitItem: Bool? = false
let isCloudItem: Bool? = false
let hasProtectedAsset: Bool? = false
}
class SongInfo: NSObject {
static let sharedData = SongInfo()
let defaults = DataDefaults()
//MARK: Properties
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
//MARK: Init
private override init () {
self.albumTitle = defaults.albumTitle!
self.albumArtist = defaults.albumArtist!
self.title = defaults.title!
self.artist = defaults.artist!
self.artwork = defaults.artwork!
self.genre = defaults.genre!
self.lyrics = defaults.lyrics!
self.releaseDate = defaults.releaseDate
self.playbackDuration = defaults.playbackDuration!
self.rating = defaults.rating!
self.assetURL = defaults.assetURL
self.isExplicitItem = defaults.isExplicitItem!
self.isCloudItem = defaults.isCloudItem!
self.hasProtectedAsset = defaults.hasProtectedAsset!
}
//MARK: Set properties
func updateData(allData: DataDefaults) {
var wasUpdated: Bool = false
if allData.albumTitle == self.albumTitle {
//pass
} else if allData.albumTitle == nil || allData.albumTitle == "" {
self.albumTitle = defaults.albumTitle!
wasUpdated = true
} else {
self.albumTitle = allData.albumTitle!
wasUpdated = true
}
//Need to repeat same IF for all properties
}
}
Is there any way I can use property name to make some reusage of the same code instead of duplicating it?
Rather than trying to find a solution to a weird design, I re-designed for what you're trying to accomplish 🙂
struct SongData: Equatable {
static let defaultData = SongData(albumTitle: "Unknown Album",
albumArtist: "Unknown Artist",
title: "Unknown Title",
artist: "Unknown Artist",
artwork: UIImage(named: "noartwork"),
genre:"",
lyrics: "No Lyrics",
releaseDate: nil,
playbackDuration: 0,
rating: 0,
assetURL: nil,
isExplicitItem: false,
isCloudItem: false,
hasProtectedAsset: false)
//MARK: Default properties
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?
/// This initializer will set the properties to the defaultData properties if a passed value is nil
init(albumTitle: String?, albumArtist: String?, title: String?, artist: String?, artwork: UIImage?, genre: String?, lyrics: String?, releaseDate: Date?, playbackDuration: TimeInterval?, rating: Int?, assetURL: URL?, isExplicitItem: Bool?, isCloudItem: Bool?, hasProtectedAsset: Bool?) {
// initialize properties where the default is nil
self.releaseDate = releaseDate
self.assetURL = assetURL
//initialize other properties with the passed values, or use the default value if nil
self.albumTitle = SongData.valueOrDefault(albumTitle, SongData.defaultData.albumTitle)
self.albumArtist = SongData.valueOrDefault(albumArtist, SongData.defaultData.albumArtist)
self.title = SongData.valueOrDefault(title, SongData.defaultData.title)
self.artist = SongData.valueOrDefault(artist, SongData.defaultData.artist)
self.artwork = artwork ?? SongData.defaultData.artwork
self.genre = SongData.valueOrDefault(genre, SongData.defaultData.genre)
self.lyrics = SongData.valueOrDefault(lyrics, SongData.defaultData.lyrics)
self.playbackDuration = playbackDuration ?? SongData.defaultData.playbackDuration
self.rating = rating ?? SongData.defaultData.rating
self.isExplicitItem = isExplicitItem ?? SongData.defaultData.isExplicitItem
self.isCloudItem = isCloudItem ?? SongData.defaultData.isCloudItem
self.hasProtectedAsset = hasProtectedAsset ?? SongData.defaultData.hasProtectedAsset
}
static func ==(leftItem: SongData, rightItem: SongData) -> Bool {
return (leftItem.albumTitle == rightItem.albumTitle) &&
(leftItem.albumArtist == rightItem.albumArtist) &&
(leftItem.title == rightItem.title) &&
// Comparing a reference type here. may need to be handled differently if that's a problem
(leftItem.artwork === rightItem.artwork) &&
(leftItem.genre == rightItem.genre) &&
(leftItem.lyrics == rightItem.lyrics) &&
(leftItem.releaseDate == rightItem.releaseDate) &&
(leftItem.playbackDuration == rightItem.playbackDuration) &&
(leftItem.rating == rightItem.rating) &&
(leftItem.assetURL == rightItem.assetURL) &&
(leftItem.isExplicitItem == rightItem.isExplicitItem) &&
(leftItem.isCloudItem == rightItem.isCloudItem) &&
(leftItem.hasProtectedAsset == rightItem.hasProtectedAsset)
}
//simple helper function to avoid long turneries in the init
static func valueOrDefault(_ value: String?, _ defaultValue: String?) -> String? {
guard let value = value, !value.isEmpty else {
return defaultValue
}
return value
}
}
class SongInfo {
static let sharedData = SongInfo()
var data: SongData
//MARK: Init
private init ()
{
self.data = SongData.defaultData
}
//MARK: Set properties
func updateData(newData: SongData) {
if(newData != self.data) {
self.data = newData
}
}
}
I changed your struct to act more like it appears you're wanting it to be used, and the struct's init will fall back to using the default values if the init values are nil. My design also contains no force unwraps, which are almost always bad.
You could set the defaults directly in your class definition without using a separate struct and have a static unaltered instance with the default values.
For example:
class SongInfo: NSObject {
static let sharedData = SongInfo()
static let defaults = SongInfo()
//MARK: Properties
var albumTitle: String? = "Unknown Album"
var albumArtist: String? = "Unknown Artist"
var title: String? = "Unknown Title"
var artist: String? = "Unknown Artist"
var artwork: UIImage? = UIImage(named: "noartwork")!
var genre: String? = ""
var lyrics: String? = "No Lyrics"
var releaseDate: Date? = nil
var playbackDuration: TimeInterval? = 0
var rating: Int? = 0
var assetURL: URL? = nil
var isExplicitItem: Bool? = false
var isCloudItem: Bool? = false
var hasProtectedAsset: Bool? = false
//MARK: Init
private override init ()
{
// nothing to do here
}
//MARK: Set properties
func updateData(allData: DataDefaults) {
var wasUpdated: Bool = false
if allData.albumTitle == self.albumTitle {
//pass
} else if allData.albumTitle == nil || allData.albumTitle == "" {
self.albumTitle = SongInfo.defaults.albumTitle!
wasUpdated = true
} else {
self.albumTitle = allData.albumTitle!
wasUpdated = true
}
//Need to repeat same IF for all properties
}
}
If you also need to manipulate the basic data without the whole class functionality, you could define a SongInfoData class with only the properties and make SingInfo inherit from that class. Then the static variable for defaults could be in the SongInfoData class and the SingInfo subclass wouldn't need any property declarations.
[EDIT] avoiding code repetition in update function ...
You can generalize the property update process by adding a generic function to your class:
For example:
func assign<T:Equatable>(_ variable:inout T?, _ getValue:(SongInfo)->T?) -> Int
{
let newValue = getValue(self)
if variable == newValue
{ return 0 }
var valueIsEmpty = false
if let stringValue = newValue as? String, stringValue == ""
{ valueIsEmpty = true }
if newValue == nil || valueIsEmpty
{
variable = getValue(SongInfo.defaults)
return 1
}
variable = newValue
return 1
}
func update(with newInfo:SongInfo)
{
let updates = newInfo.assign(&albumTitle) {$0.albumTitle}
+ newInfo.assign(&albumArtist) {$0.albumArtist}
+ newInfo.assign(&title) {$0.title}
+ newInfo.assign(&artist) {$0.artist}
+ newInfo.assign(&artwork) {$0.artwork}
+ newInfo.assign(&genre) {$0.genre}
+ newInfo.assign(&lyrics) {$0.lyrics}
// ...
if updates > 0
{
// react to update
}
}
It seems to me, that you're using MPMedia item.
If so, you don't have to store all these properties at all.
You just need to store persistent ID of the item (convert from UInt64 to string), and later fetch MPMediaItem by using MPMediaQuery with predicate, something like this:
func findSong(persistentIDString: String) -> MPMediaItem? {
let predicate = MPMediaPropertyPredicate(value: persistentIDString, forProperty: MPMediaItemPropertyPersistentID)
let songQuery = MPMediaQuery()
songQuery.addFilterPredicate(predicate)
return songQuery.items.first
}