Dear all,
I’m a german newbie and I hope you understand the way of looking at a problem.
I’m learning how to write objective c code and I’m using Xcode 7.3. One of the books I use to learn is „Objective-c and Cocoa“ from Amin Negm-Awad. The problem is, that this and all other books I know refer to Xcode 4, 5 or 6. And since then, many things have changed. But until now, research with google helped me a lot, and although it took lots of hours or days, I found an answer. But not this time. With the core data tutorial of Amin Negm-Awad’s book I discovered I do have several problems.
At first I created a new project with a core data model. Xcode generates a Model.xcdatamodeld file. Then I added an entity „Event“ with two NSString attributes „title“ and „subtitle“. Both attributes obtained a default value. Then I’ve created an NSManagedObject Subclass. The result of this were the following four files:
Event+CoreDataProperties.h:
@interface Event (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *title; @property (nullable, nonatomic, retain) NSString *subtitle;
Event+CoreDataProperties.m:
@dynamic title; @dynamic subtitle;
Event.h
Event.m
My View Controller got a Table View with the two standard column and two buttons „Add“ and „Remove". Then I wanted to drag an NSArray Controller, but the question was, where? If I drag the Array Controller to the View Controller I could connect received actions „add“ and „remove“ to my buttons, but I can’t use the managedObjectContext at the model key path. And if I drag the Array Controller to the Application I can use the managedObjectContext at key path but I’couldn’t connect the Array Controller received actions to my buttons.
I don’t know if I found THE solution, but the following solves the Errors:
ViewController.h:
@property (nonatomic, readonly) NSManagedObjectContext *managedObjectContext;
ViewController.m:
- (NSManagedObjectContext *)managedObjectContext {
return [(AppDelegate *)[[NSApplication sharedApplication] delegate] managedObjectContext];
}
So, with this lines I was able to connect the received actions of the Array Controller witch was dragged to the View Controller. And also I could use the managedObjectContext with Bind to "View Controller" with the following line at the key path in the bindings inspector:
self.managedObjectContext
At the Attributes Inspector I chose "Entity Name" for Mode and „Event“ for Entity Name. The checkbox Prepares Content I checked. Then I selected the table column in my table view and I chosed the Value in the Bindings Inspector. There I checked the box Bind To „Array Controller“ with the controller key „arranged object“.
Now, if i ran the application the result was that my add-button adds a new line in my table view with the title of the textfield in it. And with every click he adds another line with the title-text.
Table View Cell Table View Cell Table View Cell Table View Cell Table View Cell Table View Cell
So far so „good“, but now it will become very bizarre.
Now I chose the Table View Cell to bind to the Array Controller which is bound to my core data. So in the Binding Inspector I chose Value an then Bind to "Array Controller". As controller key I chose „selection“ and for the Model Key Path I wanted to use the attribute of my core data entity „title“. But the autocomplete of Xcode doesn’t show „title“.
And once again I don’t know if I found THE solution, but the following lets Xcode show my entity attributes.
I cut the @property and the @dynamic lines from the „Event+CoreDataProperties“ files and paste it into the Event files, as the following:
Event+CoreDataProperties.h:
Event+CoreDataProperties.m:
Event.h
@interface Event (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *title; @property (nullable, nonatomic, retain) NSString *subtitle;
Event.m
@dynamic title; @dynamic subtitle;
As I said, after those changes Xcode shows my entity attributes in Model Key Path in the Value Tab of the Binding Inspector, and so I chose there „title“ for Model Key Path.
But if I run the application Xcode stagnates at maybe 45 % of the build process (Building Core-Data: Core-Data | Compiling 1 of 1 Storyboard files). The ventilators of my macbook speedup and the free memory decreases. I stopped the build process but the memory decreases further. I closed Xcode an the memory decreases further. From over 8 GB to 50 MB. Then the reboot was the last thing I could do.
So if you can help me to find an Core-Data Tutorial for Xcode 7 or if you can give me some hints for solving this issues, I would be really thankful.
Many thanks an kind regards, Raycord
|