2

I have a problem storing ä, ö, ü, ß characters inside CoreData database, and later on reading them in correct form and displaying in UILabel as text. I am storing as NSString for example "äüß", but I am getting strange characters as result of reading from database. Any idea how to read correct value of NSString property?

UPDATE:

Storing to CoreData Database:

OCDatabaseFloor *floor = (OCDatabaseFloor*) [NSEntityDescription insertNewObjectForEntityForName:FLOOR_ENTITY_NAME inManagedObjectContext:managedObjectContext];
[floor setFloorName:floorName];

[self saveContext];

Save context function is saving changes to database.

Retrieving from database floor object:

NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:FLOOR_ENTITY_NAME inManagedObjectContext:managedObjectContext];
[fetch setEntity:entity];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"floorID == %d",[floorID intValue]];
[fetch setPredicate:pred];
NSArray *floors = [managedObjectContext executeFetchRequest:fetch error:nil];
return [floors objectAtIndex:0];

And class OCDatabaseFloor:

@interface OCDatabaseFloor : NSManagedObject

@property (nonatomic, retain) NSNumber * floorID;
@property (nonatomic, retain) NSString * floorName;
@end
AleksandarNS
  • 265
  • 1
  • 2
  • 15
  • 1
    How do you store and retrieve the strings right now? – JustSid Feb 27 '15 at 12:31
  • I added String attribute to CoreData Entity. Created ManagedObject subclass. Also created database manager class which is storing data to database, and getting data. – AleksandarNS Feb 27 '15 at 12:34
  • Let me mention that everything is working with English alphabet and special characters. But when I create German localisation which contain those 4 characters, they are not saved correctly with same functions. – AleksandarNS Feb 27 '15 at 12:37
  • I mean, show us the implementation (ie the code) of how you store and retrieve the strings. Any post/pre processing done on the strings? – JustSid Feb 27 '15 at 12:39
  • I updated the question with code. There is no pre/post processing of the string. I store it as is. Problem is when I use application right after storage in database everything is OK but when I restart application and read it again from database, strings are not showing as they should. – AleksandarNS Feb 27 '15 at 13:26
  • It looks correct. What kind of persistent storage do you use? Sqlite? – JustSid Feb 27 '15 at 13:46
  • No, I am using CoreData instead of Sqlite. I have problem only on localised string containing those 4 characters. Each time I store to database, and read from it to display in label. – AleksandarNS Feb 27 '15 at 13:52
  • Right, but what kind of persistent storage are you using for CoreData? – JustSid Feb 27 '15 at 13:53
  • I am using NSSQLiteStoreType. – AleksandarNS Feb 27 '15 at 14:00
  • Enable sql debugging as described in [Apple Documentation/Debugging Fetching](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdTroubleshooting.html). Both ObjC and CoreData are unicode and diacritical characters should be stored and retrieved without any special work. – thom_ek Feb 27 '15 at 14:17
  • I am constantly getting instead of ö this two strings ö.....Here is link of same issue but with contents of URL http://stackoverflow.com/questions/16166176/ignore-special-characters-like-%C3%B6-%C3%84-%C3%A9-%C3%9F-in-ios – AleksandarNS Feb 27 '15 at 14:41
  • user3532363 You should avoid using UTF8 characters in URLs - http://stackoverflow.com/questions/1386262/should-i-use-accented-characters-in-urls. – thom_ek Feb 27 '15 at 14:57
  • thom_ek I am using my application to store name to database, and then later on retrieve same name. Name contains special characters because application is based on German localisation. Special characters stored in database and then retrieved are not shown as they were added. Problem is that I get ö instead of ö, and I am not using any kind of encoding during the whole process. Just reading from label.text property and storing to nsstring property of coredata table object. – AleksandarNS Feb 27 '15 at 15:07

1 Answers1

2

Core Data on its own does not have any problem with those characters. As a test I created a demo project with one entity named Entity that has one attribute, a string called name. Then I ran this code:

NSManagedObject *obj = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:self.managedObjectContext];
[obj setValue:@"äüß" forKey:@"name"];

NSError *saveError = nil;
if (![self.managedObjectContext save:&saveError]) {
    NSLog(@"Save error: %@", saveError);
} else {
    [self.managedObjectContext reset];

    NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];

    NSError *fetchError = nil;
    NSArray *results = [self.managedObjectContext executeFetchRequest:fr error:&fetchError];
    if (results == nil) {
        NSLog(@"Fetch error: %@", fetchError);
    } else {
        for (NSManagedObject *obj in results) {
            NSLog(@"Name: %@", [obj valueForKey:@"name"]);
        }
    }
}

The results are:

2015-02-27 10:31:03.759 Junk[4808:25603285] Name: äüß

So, your string is getting corrupted, but it must be happening either before you save it to Core Data or after you read it back.

It looks like you're mixing up character encoding somewhere. You mention that "ö" gets corrupted as "ö". In UTF-8, "ö" is represented as 0xC3B6. In UTF-16, "Ã" is 0xC3 and "¶" is 0xB6. So at some point you're mixing up UTF-8 with UTF-16, but it's not happening because of Core Data.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • I have located my problem, but still didn't find out a solution. Problem is not with core data, or storing content to database. It is within received data from server which I am connected to. Received data is parsed from JSON to NSDictionary, and retrieved with valueForKey: function as NSString. Parsed string is wrong formatted. It is all encoded with UTF-8. – AleksandarNS Mar 02 '15 at 10:57