URLForResource alway returns nil - iphone

I created a project without checking "use CoreData". The Project name is "glossary"
Now I changed my mind and so for I added an data model usinng Add->New File->Resource->Data Model->gave it the name Glossary->didn`t select any class->Finish.
The next step was to design my Data Model.
Then I added the needed Code to my AppDelegate.
For all Steps i was following this Tutorial:
https://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40008305-CH1-SW1
My problem now is located in this line:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"glossary" withExtension:#"momd"]
modelURL is always nill
Creating an extra NSBundle Ref and calling mainBundle shows me that this is working, however i don`t know if it is really the right path.
Doing the same Steps on an project with selected "use CoreData" while creating works great.
Has anybody an idea?

It has to do with model versioning. You want to add a versioned model (item in Xcode project tree will have .xcdatamodeld extension) and then your code will run smoothly.
In Xcode 4 when you add a new Core Data file it is versioned by default. Even if you added Core Data support after project creation.
If you don't plan to have model versions, just use .mom in your code.

I'm not 100 % sure, but URLForResource does work for files only and momd is directory.
URLForResource documentation: The method first looks for a matching resource file in the nonlocalized resource directory of the specified bundle. (In Mac OS X, this directory is typically called Resources but in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (The search order for the language-specific directories corresponds to the user’s preferences.) It does not recurse through other subdirectories at any of these locations. For more details see Bundles and Localization.

Related

Swift 3 - Bundle.main.path with same file name in different directories

Long story short, I have two settings files that must have the same name. They're currently in separate folders, something like:
Resources/brand/dev/settings.file
Resources/brand/prod/settings.file
However I can't seem to get Swift to grab the file out, it always comes up as nil
var file = Bundle.main.path(forResource:"settings", ofType:"file", inDirectory:"Resources/brand/dev")
If I remove one of the files, and the inDirectory parameter everything works fine.
File target membership, project build phases all look OK.
Am I missing something here?
Setting files must be added to XCode with the option 'Create folder references'

Xamarin IOS - how to include the existing sqlite database file into solution

In Xamarin IOS solution, the standard code for sqlite database path is:
var sqliteFilename = "MyDatabase.db3";
string libraryPath = Path.Combine (documentsPath, "..", "Library"); // Library folder instead
var path = Path.Combine (libraryPath, sqliteFilename);
The above code is fine if the database is created from scratch.
but my question is how should I include the existing sqlite database into the solution, so it can be copied over to "Library" folder?
Include your database as a file in your solution, marking it as "Build Action -> Content". In your code, you can access it relative to the root of your project (ie, "/mydb.db3") - then read it and copy it to your Library or wherever you want to place it.
Keep in mind that files bundled into your application are READ-ONLY. You will have to copy the db to a writable path before you can write to it.

How to resolve Unable to find file.cpp in path(s) in Marmalade?

I'm just trying to begin develop a game in Marmalade (6.3). But when I have made my new sources (.cpp, and .h) and added them to the mkb, and then trying to run my program, then I got an error which says that Unable to find file.cpp in path(s). It's for all of my files except the files (game.h, game.cpp, main.cpp) which were made by Marmalade when I have chosen the new 2D game project. Should I add my .cpp and .h files to anywhere else?
Thanks
It is difficult to give a categorical answer without more info. However my guess is that you've copied and pasted from an example and not understood about the syntax of the files section. Basically:
files
{
(foo)
humbug.cpp
)
The "(foo)" might look very innocent, but it actually says that humbug.cpp is actually in directory foo - relative to the mkb file. It is common practice to actually use "(source)" and put all the source files in a directory of that name - making the source layout a bit neater.
Naturally if you have (source) and don't put the files actually in directory source, they won't be found. My guess is that is what you are seeing.
Just to clarify previous answer, The format of files directive is like this -
files
{
(<Path relative to MKB>,<Alternate Path>)
["Name of the parent Group in VS/XCode project","Name of the subparent group"]
fileName.cpp
fileName.h
}
for example I have two files SoundManager.h and SoundManager.cpp in System folder of Source, while MainMenu.h and MainMenu.cpp in Source/UI. Now the files directive would be -
files
{
(Source/System)
["Source","System"] #This part is not required, it's just to arrange your files in IDE project
SoundManager.h
SoundManager.cpp
(Source/UI)
("Source","UI")
MainMenu.h
ManinMenu.cpp
}

iOS how to localize files in for a module

I have a class that implements the "Take photo / Choose from library" that we all know and love. It is here https://github.com/fulldecent/FDTake This is included in my other projects via git submodule and that works fine.
Now I need to translate the text in that class to Chinese so it is "拍照 / 选照片" or something like that. Is there a good way to put translations in there so everyone can use them?
Localization is typically handled by the NSLocalizedString(<#key#>, <#comment#>) macro. In the source your replace all hard coded string with the macro. For example:
[self.buttonTitles addObject:#"Hi"]; // hard coded greeting
with
[self.theLabel setText:(NSLocalizedString(#"theKey", #"Hi"))];
Then genstrings (from inside a Terminal) is used to scan the implementations file (*.m) and to write its output to the language project folder (here: en.lproj)
$ genstrings -o en.lproj/ *.m
In the directory en.lproj/ a file called Localizable.strings. Its contents will be:
/* Hi */
"theKey" = "theKey";
The comment /* Hi */ is taken from our source code. The string Hi should be displayed to the (English speaking) user. So we need to edit the string on the right hand side of the equals sign and make it the greeting, = "theKey" has to become = "Hi":
/* Hi */
"theKey" = "Hi!";
So far so good
All this is fine if there are only a few strings or when there is no intend to ever modify the strings. The moment gestrings is run again it will overwrite the modifications and you effectively lose the work done in Localizable.strings.
An idea could be to write the output of genstrings to a different location. But then you will have to manually merge the changes. Once the Localizable.strings file grows it becomes a nightmare trying to keep source code and Localizable.strings in sync. So lets try to avoid that.
A big help comes from using NSLocalizedStringWithDefaultValue(<#key#>, <#tbl#>, <#bundle#>, <#val#>, <#comment#>). This macro will allow to set a default value in the Localizable.strings file and in addition, it will make the need for the initial edit of the value field go away.
Putting it together:
[self.theLabel setText:NSLocalizedStringWithDefaultValue(#"theKey2", #"Localizable", [NSBundle mainBundle], #"Hi!", #"informal greeting"))];
After running the genstrings command as used above there is now a small different in Localizable.strings
/* informal greeting */
"theKey2" = "Hi!";
Apart from the comment now telling the translator that we want an informal greeting, the “Hi!” is already present in the value filed. There is no need to go to the Localizable.strings file, search for the correct line, modify the field form “theKey” to “Hi!”. genstrings did that for us based on the default value supplied with NSLocalizedStringWithDefaultValue.
Add the file Localizable.strings to the xcode project.
Doing the translations
After changing the source code, for a new language from inside the Xcode first add a localization to Localizable.strings. Xcode will generate a copy of Localizable.strings under a subfolder based on the original Localizable.strings.
I personally don't speak Chinese, but German. So if to add German localization my translation would go under de.lproj/Localizable.strings, italian under it.lproj/ and so on.
Edit the new Localizable.strings as needed:
(German)
/* informal greeting */
"theKey2" = "Hallo!";
(Italian)
/* informal greeting */
"theKey2" = "Ciao!";
and then build and run.
********* begin edit
Bundle it up
The above considers a "standard" xcode project. You are asking about cereateing a module, therefore allowing your code to become an addition to a project. I suggest you create a bundle with the localizations. When somebody will include your code into their project the localizations remain separate.
Full documentation about bundles is here.
A project that uses bundles for localizations is QuincyKit (there probably are more, that one was the first that came to mind)
So when placing the localization into a bundle other than the mainBundle the [NSBundle mainBundle] in the line below has to change
[self.theLabel setText:NSLocalizedStringWithDefaultValue(#"theKey2", #"Localizable", [NSBundle mainBundle], #"Hi!", #"informal greeting"))];
Instead of getting the strings from the mainBundle, obtain a reference to your own module. The docs suggest:
NSBundle* myBundle = [NSBundle bundleForClass:[self class]];
So the line becomes:
[self.theLabel setText:NSLocalizedStringWithDefaultValue(#"theKey2", #"Localizable", myBundle, #"Hi!", #"informal greeting"))];
********* end edit
PS: : my original text can be seen here
I think a bundle (as Olaf suggests) would work great, but another way with less overhead to ensure that your localizable string resources don't interfere with another project in the same solution (causing a weird problem to people reusing your component in another localized project) is to change your localizable.strings file name to a unique file name. This means that where you used NSLocalizedString you now need to use its variant NSLocalizedStringFromTable (Apple documentation), where tableName is the same name as your strings file (without the .strings extension). You can define your own macro so that's it's just a straight replacement of NSLocalizedString with, say FDTakeLocalizedString.
A file name collision is much less likely to happen with XIBs files or storyboards than for the localizable.strings file. But in both cases, if you use a prefixed naming convention (say FDTake.strings and FDTake-Main.xib), it will eliminate the risk and can only help.

iPhone SDK Get tmp directory

How do i get the tmp directory on the iPhone or iPad?
if i use NSTemporaryDirectory() to create a file it does not save to the simulator temp directory.
As drawnonward said correctly in a comment
"NSTemporaryDirectory() will do the right thing on a device."
and thats true...
EDIT: Apparently there may be a problem with this approach on later Os's (>6.1.3), maybe try the NSCachesDirectory instead of the tmp directory.
As an alternative, from the documentation on NSFileManager URLForDirectory:inDomain:appropriateForURL:create:error:
You can also use this method to create a new temporary directory for storing things like autosave files; to do so, specify NSItemReplacementDirectory for the directory parameter, NSUserDomainMask for the domain parameter, and a valid parent directory for the url parameter. After locating (or creating) the desired directory, this method returns the URL for that directory. If more than one appropriate directory exists in the specified domain, this method returns only the first one it finds."