I need that I can use 1 object of NSString to use in all other files
to access 1 variable in all filees
Make this NSString a property of the AppDelegate class (or whatever your application delegate class is named). If the property is named myString, you can then access it via:
[[[UIApplication sharedApplication] delegate] myString];
To avoid warnings, you may want to import the AppDelegate class:
#import "AppDelegate.h"
...and expand the first code snippet into:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate myString];
That's an unclear question.
If I understand correctly you want to have an global NSString* shared by multiple files. In that case, in one of the source files (.m), insert
NSString* my_global_string = #"...";
and in all other source files (or in a common .h), insert
extern NSString* my_global_string;
Related
After reading quite a couple of questions in stackoverflow, I still can't find an answer at the moment.
I have trouble passing ManagedObjectContext from appdelegate to my tabbarcontroller view.
in my appdelegate.m I have this
#import "memoView.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
memoView *mView = (memoView *)navigationController.topViewController;
mView.ObjectContext =[self managedObjectContext];
}
in memoView.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"NoteLog" inManagedObjectContext:[self ObjectContext]];
[fetchRequest setEntity:entity];
NSError *error;
self.memoInfo = [ObjectContext executeFetchRequest:fetchRequest error:&error];
//self.title = #"Memo";
[fetchRequest release];
}
error reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'NoteLog''
I'm not sure what I have done wrong, I'm relative new to core data.
Any comments are appreciated.
I ran into this at one point and solved like this:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
self. ObjectContext = appDelegate.ManagedObjectContect;
Where do you put following code? I guess mView is nil when you assigning mView.ObjectContext:
#import "memoView.h"
memoView *mView = (memoView *)navigationController.topViewController;
mView.ObjectContext =[self managedObjectContext];
I'd personally implement a Singleton for my CoreData Stack. - But in a slightly safer manner like here.
The singleton pattern is basically an excuse for a "global variable". Although if implemented correctly and used wisely one of the most powerful patterns there are. Simply speaking, what it does is: It creates an object of a class IF it doesn't exist yet and stores it in a static variable of that object's class. And since a class variable's content is the same amongst all the instances of that class (obviously), the next time you try to allocate an instance of the class it checks whether it was allocated before, and if it was returns the old instance.
Thus, you can basically "allocate" from anywhere and however often you want and you'll always get the same object back.
People tend to then call that singleton class something along the lines of "DataManager" or similar.
I'm trying to share data between various viewControllers, I'm using a property declared in the appDelegate. I set the property in my first viewController and when I print out the contents, everything looks fine, but after I call a custom class method, the property gets set to some random value that seems to change every time I run the app. See code below:
appDelegate .h
NSDate *lastUpdated;
#property (nonatomic, retain) NSDate *lastUpdated;
viewController .m
AppDelegateClassName *appDelegate = (AppDelegateClassName *)[[UIApplication sharedApplication] delegate];
[appDelegate setLastUpdated:[NSDate date]];
After I set the property, I then call the following custom class method with a reference to the viewController as a parameter:
viewController .m
ForceData *forceData = [[ForceData alloc] init];
[forceData queryServiceWithParent:self];
If I try and display the contents of the appDelegate property within my custom class, it returns a random value. Once the custom class returns to the viewController, the appDelegate property stays as the random value.
I can't see what's going wrong. Can anyone help?
Thanks.
UPDATE
Here is the code within the queryServiceWithParent method:
- (void)queryServiceWithParent:(UIViewController *)controller {
viewController = (ForcesTableViewController *)controller;
responseData = [[NSMutableData data] retain];
NSString *url = [NSString stringWithFormat:#"http://example.com"];
theURL = [[NSURL URLWithString:url] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
I'm still having this problem so any help is much appreciated.
UPDATE:
I've spent some more time looking at this and I can display the contents of lastUpdated anywhere within the queryServiceWithParent (just above) and it displays fine. But if I display the same property in the connectionDidFinishLoading, it's reset. Very stuck with this one!
The way this sounds to me is that your NSDate property is autoreleased at some point, even though your memory management sounds fine the way you describe it.
Can you try to add an extra retain to your NSDate, as in:
appDelegate.lastUpdated = [[NSDate date] retain]
If that helps than we need more information about your code to find out where your memory management has errors. i.e. the complete header and main file of your appDelegate and viewController.
How does one create a global array of audioplayers so that I can call the players from other classes.
The Java equivalent to static?
I want to play multiple sounds. Is a different player needed for each, or can one player, just play random sounds throughout?
Thanks
You dont need an array of players. one player is enough
Declare this in your appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath] error:&err];
}
-(void)playAudio:(NSString *)fileName
{
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
NSString* file=[NSString stringWithFormat:#"/%#",fileName];
resourcePath = [resourcePath stringByAppendingString:file];
NSLog(#"Path : %#",resourcePath);
NSError* err;
//Declare the player in your didFinishLaunching Dont declare here
player.delegate = self;
[player play];
}
Now wherever you want to play any file create a object to appDelegate
yourAppDelegate *yourAppDelegateObject= (yourAppDelegate *)[[UIApplication sharedApplication] delegate];
Use that object to call that function
[yourAppDelegateObject playAudio:filenameOftheAudioFile];
You can create an array(NSMutableArray) in the AppDelegate class and you can add different players objects to that array, and you can access that array from any class
in App Delegate file,
in .h file
NSMutableArray *playersArray;
#property (nonatomic,retain)NSMutableArray *playersArray;
in .m file initialize that array
playersArray = [[NSMutableArray alloc] init];
in some other file you can access that array as follows
AppDelegateClassName *appDelegate = (AppDelegateClassName *)[[UIApplication sharedApplication] delegate];
[appDelegate.playersArray addObject:onePlayerObject];
similarly you can access that player object by reading from that array.
Regards,
Satya.
I have finally managed to get core data working and beginning to understand it. So far I have just been playing in a window based app with core data enabled, playing inside the app delegate files.
But how can I access my managedObjectContext from outside the app delegate, for example if I had a UIView subclass?
Try using
[[[UIApplication sharedApplication] delegate] managedObjectContext];
To get rid of warnings, cast the delegate as your actual AppDelegate; for example,
NSManagedObjectContext *context = [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
EDIT:
After you change up any data, you'll need to save it. Here's the method I use:
NSManagedObjectContext *moc = [self managedObjectContext];
NSError *error;
if (![moc save:&error]) {
NSLog(#"Couldn't save current data in current method.");
}
Change up the log statement as you see fit.
I'm getting this classic error :
The model used to open the store is incompatible with the one used to create the store
This is how it's implemented :
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:#"shotName" inManagedObjectContext:context];
NSString *newName= #"test";
[newShot setName:newName];
And this is how it's designed :
No only I'm getting a crash with the message above, I'm also getting this warning :
'NSManagedObject' may not respond to '-setName:'
Obviously something is wrong somewhere, I think I'm using Strings on both side though .
Edit, I'm now using this after Eimantas's comment :
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSManagedObject *newShot = [NSEntityDescription insertNewObjectForEntityForName:#"shotName" inManagedObjectContext:context];
NSString *newName= #"test";
[newShot setValue:newName forKey:#"shotNumber"];
[context saveAction];
But I`m still getting :
'NSManagedObjectContext' may not respond to '-saveAction'
Use setValue:forKey:
UPDATE
NSManagedObjectContext has save method, not saveAction. So:
NSError *error = nil;
[context save:&error]
if (error) {
[NSApp presentError:error];
return;
}
insertNewObjectForEntityForName:#"shotName" must be insertNewObjectForEntityForName:#"Shots". Shots is the entity name. shotName is the name of an attribute of the entity Shots. Also, like with Objective-C class names, it's standard to use singular names for your entity objects. So, Shots should be be Shot (recommended, but not required).
Also, if you change around your AppName.xcdatamodel file & generate new NSManagedObject files, you may also get the error: The model used to open the store is incompatible with the one used to create the store upon app launch. It's because it's using the old persistent store file. I call it: AppName.sqlite, but you may have a different name for this file. Search in your project for something like:
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"AppName.sqlite"]];
Then, once you know the name, to find the file, do:
find ~/Library/Application\ Support/ -name AppName.sqlite
Then, remove the file, and build & run again.