I know how to set up strings and such, but how to set up a pointer to another class? Thanks!
import UIKit
import Parse
class Tag: PFObject {
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
#NSManaged var ID: String?
#NSManaged var stanceName: String?
#NSManaged var stanceValue: NSNumber?
// HOW DO I DO SOMETHING LIKE THIS?
#NSManaged var user: PointerToUserClass?
}
The user class is special, do it this way:
#NSManaged var user: PFUser?
For other Parse objects, use:
#NSManaged var object: PFObject?
Or, if you've subclassed the Parse object, then you can use your subclass:
#NSManaged var myTag: Tag?
Related
I am trying to fetch child objects (NameCD) of the parent object (CountryCD), but I have a bug:
CountriesAPI[2441:78546] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "parent == %&"'
Is my relationship okay and how do I fix this error?
func fetchCountries() -> [CountryCD] {
do {
let fetchRequest = NSFetchRequest<CountryCD>(entityName: "CountryCD")
fetchRequest.predicate = NSPredicate(format: "parent == %&", "CountryCD")
fetchRequest.returnsObjectsAsFaults = false
let countries = try viewContext.fetch(fetchRequest)
return countries
} catch {
print("CoreDataService - fetchCountries: \(error)")
return []
}
}
import Foundation
import CoreData
extension NameCD {
#nonobjc public class func fetchRequest() -> NSFetchRequest<NameCD> {
return NSFetchRequest<NameCD>(entityName: "NameCD")
}
#NSManaged public var common: String?
#NSManaged public var official: String?
#NSManaged public var countryCD: CountryCD?
}
extension NameCD : Identifiable {
}
import Foundation
import CoreData
extension CountryCD {
#nonobjc public class func fetchRequest() -> NSFetchRequest<CountryCD> {
return NSFetchRequest<CountryCD>(entityName: "CountryCD")
}
#NSManaged public var altSpellings: [String]?
#NSManaged public var area: Double
#NSManaged public var borders: NSObject?
#NSManaged public var callingCodes: [String]?
#NSManaged public var capital: [String]?
#NSManaged public var flag: String?
#NSManaged public var latlng: [Double]?
#NSManaged public var region: String?
#NSManaged public var nameCD: NSSet?
}
// MARK: Generated accessors for nameCD
extension CountryCD {
#objc(addNameCDObject:)
#NSManaged public func addToNameCD(_ value: NameCD)
#objc(removeNameCDObject:)
#NSManaged public func removeFromNameCD(_ value: NameCD)
#objc(addNameCD:)
#NSManaged public func addToNameCD(_ values: NSSet)
#objc(removeNameCD:)
#NSManaged public func removeFromNameCD(_ values: NSSet)
}
extension CountryCD : Identifiable {
}
EDIT:
Ok I made edit and pasted two new screens. I am not sure about the relationship between CountryCD and NameCD. As you can see there is warning on the left side: abstract entity has no children. Maybe this is main problem?
I'm using Parse and I had a PFObject I was using to represent a "Job". It worked fined, but it was tedious always using setObject:forKey: and objectForKey: rather than accessing properties.
So, I decided to make a "proper" PFObject subclass. Now, every call made to "objectId" gives the above unrecognized selector error -- even calls that have nothing to do with my subclass.
I created my subclass "by the book", as far as I can tell (below), and I do call Job.registerSubclass() before Parse.setApplicationId: in my AppDelegate. Anybody seen this problem?
import Foundation
import Parse
class Job: PFObject, PFSubclassing {
#NSManaged var categoryName: String
#NSManaged var categoryId: String
#NSManaged var state: String
#NSManaged var details: String?
#NSManaged var jobDescription: String
#NSManaged var location: String
#NSManaged var dates: [String]
#NSManaged var images: PFFile?
#NSManaged var questionSequence: [String]?
#NSManaged var consumerResponseIndices: [Int]?
#NSManaged var isPosted: Bool
#NSManaged var bids: [AnyObject]?
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
class func parseClassName() -> String {
return "Job"
}
}
I got the same issue before.
You may have this error when trying to convert NSArray/NSDictionary to String type, so it turns to NSContiguousString type.
You can check:
dates
questionSequence
consumerResponseIndices
bids
to see if this happened.
In my case the problem was :
if let countryLocale = (notification.userInfo![Constants.CountryLocale]!.firstObject as? String { code }
and solved with
if let countryLocale = (notification.userInfo![Constants.CountryLocale] as! [AnyObject]).first as? String { code }
I have a User model class (generated by XCode with Swift):
#objc(User)
class User: NSManagedObject { }
And it's extension:
extension User {
#NSManaged var id: NSNumber?
#NSManaged var firstName: String?
#NSManaged var lastName: String?
#NSManaged var birthYear: NSNumber?
}
I can save/fetch data from CoreData.
But can I use this class for object management without CoreData things? Or i need to create other class/struct for this?
For example, create User object (without ObjectContext), set his attributes and send it as property in some func? Maybe i can create some struct in class User (like struct {var firstNameData, secondNameData,...}) and use it in code?
I updated class:
struct User {
var id: Int!
var firstName: String!
var lastName: String!
var birthYear: UInt?
}
#objc(UserManagedObject)
class UserManagedObject: NSManagedObject {
func toStruct() -> User {
var userData = User()
userData.id = Int(self.id)
userData.firstName = self.firstName
userData.lastName = self.lastName
if let by = self.birthYear {
userData.birthYear = UInt(by)
}
return userData
}
}
Now for object management i use struct User and UserManagedObject for CoreData in/out
I am using XCode 6.3.1 with Parse 1.7.2.
I defined the following PFObject subclass:
class MyClass: PFObject, PFSubclassing {
#NSManaged var date:NSDate!
#NSManaged var title:String!
#NSManaged var device:String!
#NSManaged var subscribers:NSArray!
#NSManaged var numberOfPictures:NSNumber!
override class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
static func parseClassName() -> String {
return "MyClass"
}
}
But when I try to instantiate this class later on in some other code:
var myObject = MyClass()
The compiler gives me an error, saying that I should specify a className parameter to the MyClass constructor.
Did I miss something here?
Parse SDK 1.7.3 seems to solve this bug.
The way you handle did not give it a class name when you initialize, it should be something like this:
var myObject = MyClass("MyClass")
Does anyone know, or have an example of, how to handle core data transient values with Swift? I know to use #NSManaged before the properties, but can't figure out how to code the logic to build the transient values using Swift.
Check mark the transient field in your data model for particular attribute(e.g. sectionTitle).
Create class for that entity, it will look something like
class Message: NSManagedObject {
#NSManaged var body: String?
#NSManaged var time: NSDate?
#NSManaged var sectionTitle: String?
}
Edit it and make it like this:
class Message: NSManagedObject {
#NSManaged var body: String?
#NSManaged var time: NSDate?
var sectionTitle: String? {
return time!.getTimeStrWithDayPrecision()
//'getTimeStrWithDayPrecision' will convert timestamp to day
//just for e.g.
//you can do anything here as computational properties
}
}
Update- Swift4
Use #objc tag for Swift 4 as:
#objc var sectionTitle: String? {
return time!.getTimeStrWithDayPrecision()
}
We should use willAccessValueForKey and didAccessValueForKey to support KVO