Post

Replies

Boosts

Views

Activity

Reply to How to filter data from an api, and how to use userdefaults to load and save data?
I would recommend using Core Data instead of UserDefaults, since UserDefaults is designed for smaller scale things like device settings for an app for example. Core Data can easily handle the movie metadata. As for the thumbnail, you can store that too but its a little more complicated. I'm not entirely sure how to do it off the top of my head, but there should be a way to store it. In terms of integrating Core Data with your user interface, you can use the NSFetchedResultsController to connect your UI where you would use the values from the fetched results controller to return as part of the UIKit implementation. I would also recommend that you use Swift's new concurrency support if you're project has an iOS/iPadOS development target of iOS/iPadOS 15.0 or later. It'll make it more difficult to make mistakes such as leaving the caller of your methods hanging by not calling the completion handler by accident.
Topic: UI Frameworks SubTopic: UIKit Tags:
Dec ’21
Reply to "Sorry, you can’t enroll at this time." when enrolling.
You have to be at least 18 years old to enrol in the Apple Developer Program yourself. There however, I believe, exists a way for you to enrol otherwise. If I am not mistaken, you can enrol using an Apple ID of someone who is over the age of 18 (parent/guardian, for example) and use their account to access the services that the Apple Developer Program provides. I would recommend double checking with Apple Developer Support before continuing.
Jan ’22
Reply to Using an @EnvironmentObject Breaks Xcode Previews
I would recommend trying to put the .environmentObject modifier on the view that you're previewing or the top level group if there are two or more of them. In the modifier you'll have to specify an instance of your environment object. Based on your code sample, you could probably just put SettingsData() in the argument for the modifier to satisfy it. Here's an example of the preview code that might fit your needs (one view) static var previews: some View { SomeView() .environmentObject(SettingsData()) } or more than one view static var previews: some View { Group { SomeView() AnotherView() // Any other views that would be placed here } .environmentObject(SettingsData()) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to SwiftUI modifying state warning - pointing to @main
Here's my custom StoredRoutes type: import Foundation final class StoredRoutes: NSObject, NSSecureCoding {     static var supportsSecureCoding: Bool = true     func encode(with coder: NSCoder) {         var routeids: [NSString] = []         for route in routes {             routeids.append(NSString(string: route.rawValue))         }         coder.encode(NSArray(array: routeids), forKey: "routes")     }     init?(coder: NSCoder) {         if let nsArray = coder.decodeObject(of: NSArray.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as! String)!)             }         } else {             return nil         }     }     var routes: [Route]     required init(routes: [Route]) {         self.routes = routes     } } and my value transformer: import Foundation final class StoredRoutesValueTransformer: NSSecureUnarchiveFromDataTransformer {          static var name = NSValueTransformerName(rawValue: "StoredRoutesValueTransformer")          override class func allowsReverseTransformation() -> Bool {         return true     }          override class func transformedValueClass() -> AnyClass {         return StoredRoutes.self     }          override class var allowedTopLevelClasses: [AnyClass] {         return [StoredRoutes.self, NSArray.self, NSString.self]     }          override func transformedValue(_ value: Any?) -> Any? {         guard let data = value as? Data else {             fatalError("Wrong data type: value must be of type Data. The type the value recieved was \(type(of: value)).")         }         return super.transformedValue(data)     }          override func reverseTransformedValue(_ value: Any?) -> Any? {         guard let storedRoutes = value as? StoredRoutes else {             fatalError("Wrong data type: value must be of type StoredRoutes. The type of the value recieved was \(type(of: value))")         }         return super.reverseTransformedValue(storedRoutes)     }     public static func register() {         let transformer = StoredRoutesValueTransformer()         ValueTransformer.setValueTransformer(transformer, forName: name)     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’22
Reply to Using Xcode with arbitrary Git repository
You can set up a repository using another provider by ticking the "Create Git repository on my Mac" option and then in the source control navigator (panel on left side, second tab), expand the category for the repository. Right click on "Remotes" and select "Add Existing Remote...". In the prompt that appears in the centre of the screen, you should be able to put in your server information and add the remote. If you already have a remote configured and everything, Xcode should be able to just see the git repository and recognise the existence of the remotes, so pushing, pulling, committing and other simpler git operations should be available from the source control menu.
Jan ’23
Reply to Core Data - Warns of unarchival of a type that is included in the permitted classes for the associated value transformer
Update: This error occurred since the custom class StoredRoutes was trying to decode an NSArray, use the methods for decoding an array instead. Older code: public init?(coder: NSCoder) {         if let nsArray = coder.decodeObject(of: NSArray.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as! String)!)             }         } else {             return nil         }     } New code that fixes this issue: public init?(coder: NSCoder) {         if let nsArray = coder.decodeArrayOfObjects(ofClass: NSString.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as String)!)             }         } else {             return nil         }     }
Jan ’23
Reply to Having difficulty connecting relationships during a data import
The issues were occurring due to bad multithreading: Make sure that you are performing the code that you intend on the correct queue. static public func refreshData(for context: NSManagedObjectContext) async throws {         let relationships = try await StationFetcher().fetch(ignoresLowDataMode: false)         var stops = relationships         var stations = relationships                  try await context.perform {             let stopsRequest = NSBatchInsertRequest(entity: Stop.entity(), managedObjectHandler: { managedObject in                 guard !stops.isEmpty else {                     return true                 }                 let stop = managedObject as! Stop                 stop.update(from: stops.removeFirst())                 return false             })                          let stationsRequest = NSBatchInsertRequest(entity: Station.entity(), managedObjectHandler: { managedObject in                 guard !stations.isEmpty else {                     return true                 }                 let station = managedObject as! Station                 station.update(using: stations.removeFirst())                 return false             })                          guard (try context.execute(stopsRequest) as! NSBatchInsertResult).result as! Bool else {                 throw StationInitializationError.unsuccessfulInsertion             }                          guard (try context.execute(stationsRequest) as! NSBatchInsertResult).result as! Bool else {                 throw StationInitializationError.unsuccessfulInsertion             }                          context.refreshAllObjects()                          for stop in relationships {                 let stationRequest: NSFetchRequest<Station> = Station.fetchRequest()                 stationRequest.predicate = NSPredicate(format: "id = %@", argumentArray: [stop.stationid])                 let stopRequest: NSFetchRequest<Stop> = Stop.fetchRequest()                 stopRequest.predicate = NSPredicate(format: "id = %@", argumentArray: [stop.stopid])                                  let stationResults = try context.fetch(stationRequest)                 let stopResults = try context.fetch(stopRequest)                                  stationResults.first!.addToStops(stopResults.first!)             }                          if context.hasChanges {                 try context.save()             }         }     }
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Older Versions of XCode
I'm sorry, however Apple are very insistent on using the latest version of macOS. The latest version of Xcode only works on macOS 13.x I think and the betas for the upcoming release of Xcode work only with macOS 13.4 and later. I would recommend updating your computer to the latest version of macOS. However, if it is not possible to do that due to lack of support (older computers typically loose support for the latest version of macOS after a number of years), I would recommend upgrading to a newer Mac that supports your needs.
Aug ’23
Reply to How to filter data from an api, and how to use userdefaults to load and save data?
Are you storing the movies themselves or just metadata about them? Also, which UI framework are you using to show your content?
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to How to filter data from an api, and how to use userdefaults to load and save data?
I would recommend using Core Data instead of UserDefaults, since UserDefaults is designed for smaller scale things like device settings for an app for example. Core Data can easily handle the movie metadata. As for the thumbnail, you can store that too but its a little more complicated. I'm not entirely sure how to do it off the top of my head, but there should be a way to store it. In terms of integrating Core Data with your user interface, you can use the NSFetchedResultsController to connect your UI where you would use the values from the fetched results controller to return as part of the UIKit implementation. I would also recommend that you use Swift's new concurrency support if you're project has an iOS/iPadOS development target of iOS/iPadOS 15.0 or later. It'll make it more difficult to make mistakes such as leaving the caller of your methods hanging by not calling the completion handler by accident.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to "Sorry, you can’t enroll at this time." when enrolling.
You have to be at least 18 years old to enrol in the Apple Developer Program yourself. There however, I believe, exists a way for you to enrol otherwise. If I am not mistaken, you can enrol using an Apple ID of someone who is over the age of 18 (parent/guardian, for example) and use their account to access the services that the Apple Developer Program provides. I would recommend double checking with Apple Developer Support before continuing.
Replies
Boosts
Views
Activity
Jan ’22
Reply to Using an @EnvironmentObject Breaks Xcode Previews
I would recommend trying to put the .environmentObject modifier on the view that you're previewing or the top level group if there are two or more of them. In the modifier you'll have to specify an instance of your environment object. Based on your code sample, you could probably just put SettingsData() in the argument for the modifier to satisfy it. Here's an example of the preview code that might fit your needs (one view) static var previews: some View { SomeView() .environmentObject(SettingsData()) } or more than one view static var previews: some View { Group { SomeView() AnotherView() // Any other views that would be placed here } .environmentObject(SettingsData()) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to SwiftUI not rendering the background view for List when selected in edit mode
This issue has appeared to have been fixed in a subsequent update.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to SwiftUI modifying state warning - pointing to @main
Here's my custom StoredRoutes type: import Foundation final class StoredRoutes: NSObject, NSSecureCoding {     static var supportsSecureCoding: Bool = true     func encode(with coder: NSCoder) {         var routeids: [NSString] = []         for route in routes {             routeids.append(NSString(string: route.rawValue))         }         coder.encode(NSArray(array: routeids), forKey: "routes")     }     init?(coder: NSCoder) {         if let nsArray = coder.decodeObject(of: NSArray.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as! String)!)             }         } else {             return nil         }     }     var routes: [Route]     required init(routes: [Route]) {         self.routes = routes     } } and my value transformer: import Foundation final class StoredRoutesValueTransformer: NSSecureUnarchiveFromDataTransformer {          static var name = NSValueTransformerName(rawValue: "StoredRoutesValueTransformer")          override class func allowsReverseTransformation() -> Bool {         return true     }          override class func transformedValueClass() -> AnyClass {         return StoredRoutes.self     }          override class var allowedTopLevelClasses: [AnyClass] {         return [StoredRoutes.self, NSArray.self, NSString.self]     }          override func transformedValue(_ value: Any?) -> Any? {         guard let data = value as? Data else {             fatalError("Wrong data type: value must be of type Data. The type the value recieved was \(type(of: value)).")         }         return super.transformedValue(data)     }          override func reverseTransformedValue(_ value: Any?) -> Any? {         guard let storedRoutes = value as? StoredRoutes else {             fatalError("Wrong data type: value must be of type StoredRoutes. The type of the value recieved was \(type(of: value))")         }         return super.reverseTransformedValue(storedRoutes)     }     public static func register() {         let transformer = StoredRoutesValueTransformer()         ValueTransformer.setValueTransformer(transformer, forName: name)     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to Bizarre CoreData error when moving to data model to swift package
Changing the module to the swift package instead of the current project module worked for me.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’22
Reply to Referencing initializer 'init(_:content:)' on 'ForEach' requires that 'Message' conform to 'Identifiable'
If your Message type has an id property, you just need to add the protocol conformance for Identifiable. struct Message: Identifiable /* Other protocols here */ { var id = UUID() /* Rest of implementation here... */ }
Replies
Boosts
Views
Activity
Jan ’23
Reply to Using Xcode with arbitrary Git repository
You can set up a repository using another provider by ticking the "Create Git repository on my Mac" option and then in the source control navigator (panel on left side, second tab), expand the category for the repository. Right click on "Remotes" and select "Add Existing Remote...". In the prompt that appears in the centre of the screen, you should be able to put in your server information and add the remote. If you already have a remote configured and everything, Xcode should be able to just see the git repository and recognise the existence of the remotes, so pushing, pulling, committing and other simpler git operations should be available from the source control menu.
Replies
Boosts
Views
Activity
Jan ’23
Reply to Core Data - Warns of unarchival of a type that is included in the permitted classes for the associated value transformer
Update: This error occurred since the custom class StoredRoutes was trying to decode an NSArray, use the methods for decoding an array instead. Older code: public init?(coder: NSCoder) {         if let nsArray = coder.decodeObject(of: NSArray.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as! String)!)             }         } else {             return nil         }     } New code that fixes this issue: public init?(coder: NSCoder) {         if let nsArray = coder.decodeArrayOfObjects(ofClass: NSString.self, forKey: "routes") {             self.routes = []             for id in nsArray {                 self.routes.append(Route(rawValue: id as String)!)             }         } else {             return nil         }     }
Replies
Boosts
Views
Activity
Jan ’23
Reply to Having difficulty connecting relationships during a data import
The issues were occurring due to bad multithreading: Make sure that you are performing the code that you intend on the correct queue. static public func refreshData(for context: NSManagedObjectContext) async throws {         let relationships = try await StationFetcher().fetch(ignoresLowDataMode: false)         var stops = relationships         var stations = relationships                  try await context.perform {             let stopsRequest = NSBatchInsertRequest(entity: Stop.entity(), managedObjectHandler: { managedObject in                 guard !stops.isEmpty else {                     return true                 }                 let stop = managedObject as! Stop                 stop.update(from: stops.removeFirst())                 return false             })                          let stationsRequest = NSBatchInsertRequest(entity: Station.entity(), managedObjectHandler: { managedObject in                 guard !stations.isEmpty else {                     return true                 }                 let station = managedObject as! Station                 station.update(using: stations.removeFirst())                 return false             })                          guard (try context.execute(stopsRequest) as! NSBatchInsertResult).result as! Bool else {                 throw StationInitializationError.unsuccessfulInsertion             }                          guard (try context.execute(stationsRequest) as! NSBatchInsertResult).result as! Bool else {                 throw StationInitializationError.unsuccessfulInsertion             }                          context.refreshAllObjects()                          for stop in relationships {                 let stationRequest: NSFetchRequest<Station> = Station.fetchRequest()                 stationRequest.predicate = NSPredicate(format: "id = %@", argumentArray: [stop.stationid])                 let stopRequest: NSFetchRequest<Stop> = Stop.fetchRequest()                 stopRequest.predicate = NSPredicate(format: "id = %@", argumentArray: [stop.stopid])                                  let stationResults = try context.fetch(stationRequest)                 let stopResults = try context.fetch(stopRequest)                                  stationResults.first!.addToStops(stopResults.first!)             }                          if context.hasChanges {                 try context.save()             }         }     }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jan ’23
Reply to Enrollment as an Organization
Is your organisation registered in the country in which you are operating?
Replies
Boosts
Views
Activity
Jan ’23
Reply to NSExpression syntax for mapping
I've done some restructuring and have removed the CDRoute wrapper and replaced it with a different wrapper called StoredRoutes which wraps an array of the Route enum. Is there a way to access sub properties of the [Route] using NSExpression?
Replies
Boosts
Views
Activity
Jan ’23
Reply to Older Versions of XCode
I'm sorry, however Apple are very insistent on using the latest version of macOS. The latest version of Xcode only works on macOS 13.x I think and the betas for the upcoming release of Xcode work only with macOS 13.4 and later. I would recommend updating your computer to the latest version of macOS. However, if it is not possible to do that due to lack of support (older computers typically loose support for the latest version of macOS after a number of years), I would recommend upgrading to a newer Mac that supports your needs.
Replies
Boosts
Views
Activity
Aug ’23
Reply to Xcode 15.0 Beta 5, Symbol not found: _$s9SwiftData014DefaultBackingB0C3forACyxGxm_tcfC
I personally would try cleaning the build folder and then rebuilding to see if that fixes the problem. If that does not work, could you provide more context about where the provided code is used?
Replies
Boosts
Views
Activity
Aug ’23