Xcode Localization returning Key instead of value - xcode

I am adding localization support in my project where I have added Localizable.strings file, and adding key value pair as below:
"not_found" = "device not found";
I am then adding a label to my viewcontroller file as below:
self.notificationTextLabel.text = [NSString stringWithFormat:NSLocalizedString(#"not_found", nil)];
When I run the app label shows not_found instead of "device not found". I have followed tutorials and still not able to figure out why the key is printed. What I could be doing wrong?
Here's what my Project navigator shows:
each contains single line as shown above. French version has translated string.
Also the storyboard also has localizations enabled and has .strings files:
Update:
There is a similar question with not very helpful answers.
Localization issue Xcode

This happens if the localization isn't found.
One problem is that if the compiler finds any problem in a localizable.strings file, that entry and all following entries are silently ignored. So look what's in the file, and see if other entries earlier in the file work.
BTW. [NSString stringWithFormat... ] expects a format string. What you are doing will get you into all kinds of trouble if you ever use a translation that contains a percent character. Just write
self.notificationTextLabel.text = NSLocalizedString(#"not_found", nil);

Found the solution! It was something unexpected. I had created a localizable.strings file earlier and It was conflicting with the newly created file. I deleted all new files and kept only Original file. Works like a charm now!

Related

How to add correct key:value pair to Xcode info.plist

I am trying to add an info string and description for NSPhotoLibraryUsageDescription as the error describes below. However, I have never used Xcode before, and apparently how I adding the key and value are incorrect. The key is setting to default item 1 so I have to put NSPhotoLibraryUsageDescription in the value. so obviously its not working. What is the workaround in this case?
ITMS-90683: Missing Purpose String in Info.plist - Your app's code references one or more APIs that access sensitive user data. The app's Info.plist file should contain a NSPhotoLibraryUsageDescription key with a user-facing purpose string explaining clearly and completely why your app needs the data.
Same issue today for me.
It's not the first time that Apple send us that issue after uploading build.
It looks like their automated checks fails sometimes.
Last time we've waited few hours and submitted again a new build (same source code) and it worked.
[EDIT]
Following https://developer.apple.com/forums/thread/680170 it seems that Apple changed their checking rules. We have InfoPlist.strings file in our Watch extension and builds were processed in the past. Now it requires missing keys to be in the Info.plist alongside any localisation file to complete processing builds.

CoreData codegen toolchain error with Xcode 8.1 [duplicate]

I am getting this error on generating an NSManagedObject in Xcode 8.1 in Swift.
:0: error: filename "DemoOne+CoreDataClass.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataClass.swift' and '/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataClass.swift'
:0: note: filenames are used to distinguish private declarations with the same name
:0: error: filename "DemoOne+CoreDataProperties.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataProperties.swift' and '/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataProperties.swift'
:0: note: filenames are used to distinguish private declarations with the same name
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
I searched for this and got answers saying the file might be present twice in the project which is not my case. Another solution I came across was to remove the file from the build phase and add again. That too didn't work. Is it some build setting configuration mistake ? Or something else?
Here is the link with a demo project created with Xcode 8.1 and showing this problem:- https://www.dropbox.com/s/xwyzhshktb2hqe7/demo2.zip?dl=0
The issue is happening because of the Xcode automatic subclass generation feature.
According to What's New In Core Data
Xcode automatic subclass generation
Xcode now supports automatic generation of NSManagedObject subclasses
in the modeling tool. In the entity inspector:
Manual/None is the default, and previous behavior; in this case you
should implement your own subclass or use NSManagedObject.
Category/Extension generates a class extension in a file named like
ClassName+CoreDataGeneratedProperties. You need to declare/implement
the main class (if in Obj-C, via a header the extension can import
named ClassName.h).
Class Definition generates subclass files named
like ClassName+CoreDataClass as well as the files generated for
Category/Extension.
The generated files are placed in DerivedData and
rebuilt on the first build after the model is saved. They are also
indexed by Xcode, so command-clicking on references and fast-opening
by filename works.
So for fixing your issue, you need to set the Codegen field value to Manual/None as shown below:
This is what I did to fix this issue,
Select your entity in the data model
Set the Module to Current Product Module
Leave the Codegen to Class Definition though Manual/None also works
Select Editor > Create NSManagedObject Subclass
May help someone, Go to project compile source delete the extra reference if any.
Select code data model then select the Entity change the value of 'Codegen' to Manual/None.
It will work.
The issue was due a second file named:"Extensions.swift" in my project.
After deleting the duplicate "Extensions.swift" the error got resolved. Your error message might tell you which file is duplicated.
In my case Data Model (as example: MyCoreData.xcdatamodeld) was listed twice under Editor -> Create NSManagedObject Subclass... And here was reason why it was getting this type of error.
What I did:
I did delete MyCoreData.xcdatamodeld file.
Created another Data Model as example MyCD.xcdatamodeld.
Checked is here duplicated Data Models in here: Editor -> Create NSManagedObject Subclass...
Clean and later build for two times.
It happened to me when I copied and renamed entity. It failed to rename the class, so do it manually.
I just restart my Macbook and that clean my build folder. And it works like a charm!

How to register a custom non-document UTI / file type for OSX to recognize it?

I've read all the tickets about this issue, but I still don't get it right. I have a non-document OSX app (for OSX Lion and MountainLion). I want this app to export and import custom data, associated with a custom file extension ".iobs". Internally, these files are just data archived with [NSKeyedArchiver archivedDataWithRootObject:], and saved onto the disk with the "iobs" extension.
So, my check list is this:
1) Export mechanism: checked. My app create correctly .iobs files. If I run "file <filename.iobs>" in the Terminal, I get "iObserve_exportedItems.iobs: Apple binary property list"
2) Declaration of an exported UTI, checked. As shown in the image below. I did NOT declared a custom Document type, since it I never use NSDocument inside my app, and there is no point. Anyway, I already tried and failed. I've tried also different combinations of "Conforms To" entries, but with no success.
3) Is there any 3rd point??? Do I need to start my app once to let the system know? I just ran it in Debug from Xcode so far, and this has no effect. So I guess my Info.plist is wrong, but I filled it from within Xcode4 interface, so???
Thanks for any help, hint, question, suggestion.
Ok, so apparently, I do have to declare a document type even if I don't specify a document class. See the attached screenshot. Note that leaving only the Document UTI doesn't work. I do need the two (exported UTI and document type). Note also that if I say it conforms to com.apple.binary-property-list, I don't have the right icon.
And for those who wonder, there is nothing to do to "register" a type (and its subsequent changes) apart from launching the app once.

Mac OSX 10.7 Lion AUPinkNoise does not compile

Apple Technical Note TN2276 lists changes to audio unit code for Lion. I successfully applied the changes to FilterDemo sample and it compiles ok. Then I apple same changes to AUPinkNoise sample, but I get a linker error says undefined symbol _FilterFactory. I changed code in AUPinkNoise.cpp from
COMPONENT_ENTRY(AUPinkNoise)
to
AUDIOCOMPONENT_ENTRY(AUBaseFactory, AUPinkNoise)
But that does not help. Being new to core audio I have no idea how to fix this. Any ideas?
I had a similar problem, except with another audio unit project. From your description of the error, I think you forgot to edit the .exp file. In the .exp file you should have _"name"Entry and _"name"Factory. For example, in your case in the .exp file (located in the AUSource folder) it should be as follows
1:_AUPinkNoiseEntry
2:_AUPinkNoiseFactory
3:
Remember to have a blank line at the very end.
Also, you will want to edit the info.plist file as well by adding an AudioComponents Array entry and add AUPluginDispatch.cpp/h (located in the AUBase folder).

xcode localized string not loaded

I have met a strange problem with the localized strings. I have only a 'Localizable.strings' in my 'en.lproj' folder and it works fine. all the strings are shown on device. but the next time i compile it and run, it shows only the ID of the strings. even if i change nothing and only click on build&debug. and the next time it works fine again and next time again shown with IDs.
so does anyone knows why this is happening? it's kind of annoying that i always need to build twice.
My SOLUTION is at bottom:
I've been running into the same problem: Alternating runs yield correct, then incorrect translations (only for English though).
Adding "-NSShowNonLocalizedStrings YES" as an argument to the app yielded:
Localizable string "MyKey" not found in strings table "Localizable" of bundle CFBundle
So, I tried loading the key file directly from the bundle as a string and dumping it. Well, the times it did NOT work correctly, it was displaying a bunch of built-in iOS messages. So, I went to the APP file that was built, opened the package contents, and viewed the en.lproj/Localizable.strings file...and voila!!! The file had been filled with Apple iOS key/value pairs. On the next build, it was filled as expected.
Of course, this has nothing to do with the encoding of the files (which should be UTF-16). I have not been able to locate anything with mention of this specific problem.
MY SOLUTION:
I copied the contents of the legitimate english Localizable.strings file FROM THE APP PACKAGE (not from my source) into an XML file (when compiled, the .strings file are converted into XML) and added to my project. I then loaded this file into a dictionary at startup, and if the call to NSLocalizedString returned the key instead of the value, I did a lookup on the dictionary I loaded. In theory, you could do this for all languages, but I was only having the problem with english.
Yes, it's not the answer to the problem, but it's a workaround.
Check if you have more than one Localizable.strings in your project. Merging them into one solved it for me. (Check any external code you use e.g. ShareKit)

Resources