iosarraysswiftcore-datacgpoint

store Array containing CGPoints in CoreData database (Swift)


so as the title already states I'm trying to save an array to the database. If this is possible, how do I do it? If not I hope you can help me with some other solution.

I am making an iOS app where if the user touches (and moves) the screen I store this data in an array. Because it needs to be multi-touch all the CGPoints of the touches (either touchesBegan or touchesMoved) on one moment are stored in an array, which again is stored in the main array. Resulting in var everyLocation = [[CGPoint]](). I already found out that it's not possible to store a CGPoint in a database directly, so I can convert them to string with NSStringFromCGPoint(pointVariable). This however isn't very useful as long as I can't store the array... I want to store the date on which it happened too, so in my database I created the entity 'Locations' with two attributes: 'locations' and 'date'. In the final application the entity name will be the name of the exercise the user was doing (I have about four exercises, so four entities). Most of the sample code I've seen stores the CGPoint either in a separate x and y or in one string. I can maybe do this too, so I don't have to store arrays. To do this I think I will have to make the attribute(s) the coordinates of the touche(s), the entity name would be the date, and the db name would be the name of the exercise. If this is the only solution, how do I create an entity (with attributes) at run-time?

Thanks in advance


Solution

  • 1) add a "Transformable" type attribute.

    enter image description here

    2) Event.h

    @interface Event : NSManagedObject
    
    @property (nonatomic, retain) NSArray * absentArray;
    @interface AbsentArray : NSValueTransformer
    
    @end
    

    Event.m

    @implementation AbsentArray
    
    + (Class)transformedValueClass
    {
        return [NSArray class];
    }
    
    + (BOOL)allowsReverseTransformation
    {
        return YES;
    }
    
    - (id)transformedValue:(id)value
    {
        return [NSKeyedArchiver archivedDataWithRootObject:value];
    }
    
    - (id)reverseTransformedValue:(id)value
    {
        return [NSKeyedUnarchiver unarchiveObjectWithData:value];
    }
    
    @end
    

    3) Just use it as a normal array

    Event *event = //init
    event.absentArray = @[1,2,3];
    [context save:nil]
    

    Just change these code in swift.

    You can understand as .swfit combine .h/.m file. Objective C has .h as header file which many properties there. .m is implication file which methods should be there.

    For example: .swift

    import Foundation
    import CoreData
    
    class Event: NSManagedObject {
    
        @NSManaged var absentArray: AnyObject
    
    }
    

    3) save:

    let appDelegate =
      UIApplication.sharedApplication().delegate as AppDelegate
    
      let managedContext = appDelegate.managedObjectContext!
      if !managedContext.save(&error) {
      println("Could not save \(error), \(error?.userInfo)")
     }