Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Swift NSSet & CoreData

for aresponse in
self.response.question.responses {
    // this next line throws an error
    var scoreVal = aresponse.score.integerValue

The problem is that for aresponse in ... responses is iterating through an NSSet. Swift doesn't know what is in the NSSet, so it treats each value aresponse as an AnyObject, which has no properties. Thus, the compiler complains when you try to get the score property of aresponse.

You, on the other hand, know that each value aresponse is a Response. So you need to tell Swift this, by casting (with as). Note that you were in fact doing that in your Objective-C code:

for(Response *aresponse in

But you have forgotten to do the same thing in Swift. You can do it in various ways; for example you could write this sort of thing:

for aresponse in
self.response.question.responses {
    var scoreVal = (aresponse as

That gets us past the compiler, which now shares in our knowledge of what this variable really is.

Once you get used to this, you will be kissing the ground that Swift walks on. All this time, in Objective-C, you were making assumptions about what kind of object you had, but in fact what you had was an id and the compiler was letting you send any message at all to it - and so, you would often crash with "unrecognized selector". In Swift, an "unrecognized selector" crash is basically impossible because the type of everything is specified and the compiler won't let you send an inappropriate message to an object in the first place.

Categories : IOS

Related to : Swift NSSet & CoreData
Issue using CoreData in Swift
You need to fetch the data from the database: let fetchRequest = NSFetchRequest(entityName:"SavedData") var error: NSError? let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [data]? if let resultsArray = fetchedResults { if resultsArray.count > 0 { let newItem = resultsArray[0] as data println("Saved data is (newItem.test)") } e

Categories : IOS
SWIFT - Coredata Delete relational records
I believe pbasdf is correct. If you're trying to remove B objects (where A to B is a one-to-many relationship) by calling function removeList(values: NSSet), you need to use NSManagedObjectContext.deleteObject() which actually removes the object from data store. If you think about it, NSMutableSet.removeObject() only removes the object from the NSSet and NOT from object store. So the entry you

Categories : IOS
TableView CoreData error: Assertion failure in ... in Swift
used tabeView in place of tableViewNotes func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell:MyTableViewCell = tabeView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MyTableViewCell cell.textLabel.text = "(daten[indexPath.row].aufgabe)" return cell } Let me know if it works.

Categories : IOS
CoreData not firing faults
It's hard to answer this kind of question without seeing the actual code. However it appears your fetchResultController's results are being changed during the navigation. A good practice would be, try to execute your fetchRequest and feed it to the fetchResultController in your ViewController's viewWillAppear.

Categories : IOS
CoreData tips for good implementation
As you probably already know CoreData is not threadsafe per se. So if you are working with your database on different threads you probably need to take more care of your database stack. In personal experience I think a 3-layer core data stack is more appropriate for a large scale app. (see: ,

Categories : IOS
Recently Add
keychain iOS touchId prompted twice
-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0'
Search Results Visibly Scrolling Underneath the Status Bar
How do I exclude an IOS 8 app extension from a build?
EXC_BAD_ACCESS when capturing a GPU frame
iOS 8 - Map keeps reanimating back to user location
Constantly refreshing background image of UIImageView IOS
UINavigationController keeps displaying popped view
Swift closure syntax using Shark Food Mute Switch?
MagicalRecord 3.0 not saving. No objects for the second build
iOS / Swift Can't add gesture to child class
iOS: Is there currently a way to prevent two view controllers being pushed or popped at the same time?
Checking if array contains two objects
How should turning of SCNLight affect background color?
Why don't my views show up in Storyboards and are greyed out in the left pane?
SWIFT convert NSTimeInteval to NSDate
BLE scanning is stopped when go to background?
Drawing a Rect on UIView
HLS playback via local HTTP server on iOS
Re-Assigning SKScene delegate protocol
Network lost on screen lock unlock
Annoying Xcode Error "file not found" #import
CoreData not firing faults
iOS:How do you change a view size/origin from another view controller class?
Changing a frame in viewDidLayoutSubviews
Animate multiple shapes in UIView
Add NSIntegers ONLY when 2nd NSInteger Changes Value
Facebook SDK, fetching friends
How to get Storyboard to support dynamically sized cells in UICollectionView?
© Copyright 2017 Publishing Limited. All rights reserved.