If I have a bundle that contains a class and some resources used by that class. If I load the class from the bundle how should I load the resources(that are in the bundle where I loaded the class from) in that class?
Let's say I want to load an image inside my object instantiated from the class loaded from the bundle. If I do
NSImage *image = [NSImage imageNamed:@"myImage"];
Will it load the image that's inside the bundle from where I loaded the class from? or will it look in the bundle of the application that loaded the bundle with the class and resources?
It's called contentsOfFile , and here it is in action: if let filepath = Bundle. main. path(forResource: "example", ofType: "txt") { do { let contents = try String(contentsOfFile: filepath) print(contents) } catch { // contents could not be loaded } } else { // example.
A resource bundle is a set of properties files with the same base name and a language-specific suffix. For example, if you create file_en. properties and file_de. properties, IntelliJ IDEA will recognize and combine them into a resource bundle.
Simply put, the ResourceBundle enables our application to load data from distinct files containing locale-specific data.
In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().
On OS X 10.7+ do this do this:
NSBundle *otherBundle = [NSBundle bundleWithIdentifier: @"com.company.otherapp"];
NSImage *imageFromOtherBundle = [otherBundle imageForResource: @"imageName"];
This method searches for named images in several places, returning the first image it finds matching the given name. The order of the search is as follows:
- Search for an object whose name was set explicitly using the setName: method and currently resides in the image cache.
- Search the application's main bundle for a file whose name matches the specified string. (For information on how the bundle is searched, see ““Accessing a Bundle's Contents”“ in Bundle Programming Guide.)
- Search the Application Kit framework for a shared image with the specified name. When looking for files in the application bundle, it is better (but not required) to include the filename extension in the name parameter. (Link)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With