Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
Im not sure you understand. I need to declare an NSMutableArray and then be able to access (run instance methods) a single instance of it from anywhere in the application.
The way to declare a global is to declare it outside of any @interface...@end block, and to define in outside of any @implementation...@end block, as in:
//this code goes under some .h file (it doesn't matter which one) extern NSMutableArray *globalArray;
@interface (of some class - it doesn't matter which one) ... @end
Then define globalArray as in:
NSMutableArray *globalArray; @implementation (any, it doesn't matter which one) ... @end
And then, somewhere where you are sure it will only be executed once:
globalArray = (some alloc-type of NSMutable Array creation method, retaining)
Now you have a singleton NSMutableArray you can access from anywhere. Just don't ever release it. It's memory will be reclaimed automatically when your app closes.
I have been reading your reply to this post and would like to use it in my App.
What I am trying to do is read the content of a record held in an SQLite table and use the result in a number of views. The SQLite result is currently written into an array. At the moment I display the result in a UIScrollView and it works fine but the amount of scrolling is becoming far to long.
I thought I would split the result between separate views. Reading your reply within the post I thought I would try and use a global array.
The code which creates my current array is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic -- create and push a new view controller
// Create a new country object with the data from the database Country *country = [[Country alloc] initWithName:aName countryName:aCountry capitalCity:aCapital firstLang:aLanguageF secondLang:aLanguageS voltage:aVoltage freq:aFreq dialCode:aDialCode currencyCode:aCurrencyCode currencyName:aCurrencyName currencySymbol:aCurrencySymbol phonePolice:aPhonePolice phoneMedical:aPhoneMedical phoneFire:aPhoneFire countryFlag:aCountryFlag countryKey:aCountryKey countryOutletType:aCountryOutlet timeZone1:atimeZone1 timeZone2:atimeZone2 timeZone3:atimeZone3];
// Add the country object to the countries Array [countries addObject:country];
[country autorelease];
// Setup the animation [self.navigationController pushViewController:self.countryView animated:YES]; // Set the title of the view to the countries name self.countryView.title = [country countryName];
} // Release the compiled statement from memory sqlite3_finalize(compiledStatement);
}
In the .h I have made a change from "NSMutableArray *countries" which was inside of the @interface...@end block to "extern NSMutableArray *countries;" which is now outside of the @interface...@end bloc. I have also declared "extern NSMutableArray *countries;" before the @implementation section in the .m file.
The part I am not clear about is:
globalArray = (some alloc-type of NSMutable Array creation method, retaining)
Can you advise me how to implement my array in to this statement so I can use it in other views.
Many thanks for any advice and help you can provide.
from "NSMutableArray *countries" which was inside of the @interface...@end block to "extern NSMutableArray *countries;" which is now outside of the @interface...@end block
you are saying that countries, which used to be an instance variable of the class, is now a global variable. So countries is what you want to use instead of globalArray.
Next, you should not use "extern" in the .m file. "extern" is only for the declaration in the .h file, not for the actual variable definition in the .m file.
Finally, you already created countries and it is retained (because you used alloc and did not release it).
So if you take away that one unnecessary extern, then you would have the setup you need to make countries a global NSMutableArray. To access it in other views, only need to import the .h file that declares countries into the .m file of those views.
I should warn you that using a global variable like this invites problems if you are careless. Remember that countries is not actually the NSMutableArray itself. It is just a pointer to that NSMutableArray. Like all Objective-C objects, this object can only be accessed through pointers. And the memory that it points to needs to be dynamically managed. Normally this dynamic memory management is taken care of for you when you make a property of an object.
By looking at your code, I see that you set countries in response to a didSelectRowAtIndexPath. Therefore you might set countries more than once. When you do that, the old NSMutableArray that countries used to point to becomes leaked memory, unless you first release it. This would have been handled automatically for you if you were content to use properties and the standard retaining setter that comes with it. But by implementing a global variable, you have abandoned the use of properties. So you have to do the work that the properties would have done for you automatically. If you really understand the retain/release system and are committed to taking the care needed to release dynamically allocated memory at just the right time, then you can make this work. But this is not normally done.
OK, I have done my best to warn you against using a global variable as a pointer to a NSMutableArray. So the choice is up to you.
Many thanks for such a details response. After reading tier reply I think I need to look at how I am going the get the result I need in another way.
Because "countries" will be set more than once your right in saying it's not the way to do it. The only reason I was looking at using a global array is because I cannot think of any other way to retain the variables in each of the views.
From what I have read, "and since I started trying to learn Objective C, which was back in March, I've done a lot of reading but not enough by the look of it" your suggestion is a great idea. The only problem I have is I am not sure how to implement it.
Do you have the time to show me the syntax I should use to make the "countries" array a property of the App delegate and how I would make a call to it from a UIView.
I think if I can get over this issue I may be home and dry.
See the end of my tutorial on globals here. Just use NSMutableArray countries instead of NSString userName. And never set the value of countries except with the setter:
I have a file which reads my SQL data and creates an ARRAY containing the record requested by the user. A part of the file is below:
// Create a new country object with the data from the database Country *country = [[Country alloc] initWithName:aName countryName:aCountry capitalCity:aCapital firstLang:aLanguageF secondLang:aLanguageS voltage:aVoltage freq:aFreq dialCode:aDialCode currencyCode:aCurrencyCode currencyName:aCurrencyName currencySymbol:aCurrencySymbol phonePolice:aPhonePolice phoneMedical:aPhoneMedical phoneFire:aPhoneFire countryFlag:aCountryFlag countryKey:aCountryKey countryOutletType:aCountryOutlet timeZone1:atimeZone1 timeZone2:atimeZone2 timeZone3:atimeZone3];
// Add the country object to the countries Array [countries addObject:country]; appDelegate.countries = countries; [country autorelease];
The part I am struggling with is assigning the content of the ARRAY to the "appDelegate.countries = countries". I know this is because I don't know how this is done even thou I have read and re-read your tutorial. Basically I am having a very thick head time.
Can you help me advance this issue a little further.
I have gotten a little further, I now have the following in the file which reads the country record in to the array. I have changed he name of the array from countries to countriesList to avoid a clash with another var.
// Create a new country object with the data from the database Country *country = [[Country alloc] initWithName:aName countryName:aCountry capitalCity:aCapital firstLang:aLanguageF secondLang:aLanguageS voltage:aVoltage freq:aFreq dialCode:aDialCode currencyCode:aCurrencyCode currencyName:aCurrencyName currencySymbol:aCurrencySymbol phonePolice:aPhonePolice phoneMedical:aPhoneMedical phoneFire:aPhoneFire countryFlag:aCountryFlag countryKey:aCountryKey countryOutletType:aCountryOutlet timeZone1:atimeZone1 timeZone2:atimeZone2 timeZone3:atimeZone3];
// Add the country object to the countries Array [countries addObject:country];
iTravelv113AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; UITextView.acountryName = appDelegate.countries; //..to fetch NSLog(@\"The contents of countries = %@\",[countries description]); }
but I am getting the following error: "Accessing unknown setAcountryName class method, Object cannot be set, either readonly property or setter cannot be found".
acountryName is not a property of UITextView. Why is UITextView there?
That's a good question and to be truthful I don't know. What I was trying to do is make the "acountryName" available so I could eventually use it in a view.
Assuming all the other code is correct, How do I call the content of the "countries" array so it can be used the the view I am working on.
I know I'm a pain in the neck, sorry. But I am learning fast, I think.
Replies
Hi again btw.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHow?!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThe best way to learn is to do.
My Apps:
<a href="http://itunes.apple.com/us/app/ipi
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAvailable on the App Store
Fast Flash Cards</f
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI Hope This Helps & Happy Coding,
GetOffTheCourt
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThen define globalArray as in:
And then, somewhere where you are sure it will only be executed once:
Now you have a singleton NSMutableArray you can access from anywhere. Just don't ever release it. It's memory will be reclaimed automatically when your app closes.
Robert Scott
Ypsilanti, Michigan
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI have been reading your reply to this post and would like to use it in my App.
What I am trying to do is read the content of a record held in an SQLite table and use the result in a number of views. The SQLite result is currently written into an array. At the moment I display the result in a UIScrollView and it works fine but the amount of scrolling is becoming far to long.
I thought I would split the result between separate views. Reading your reply within the post I thought I would try and use a global array.
The code which creates my current array is:
In the .h I have made a change from "NSMutableArray *countries" which was inside of the @interface...@end block to "extern NSMutableArray *countries;" which is now outside of the @interface...@end bloc. I have also declared "extern NSMutableArray *countries;" before the @implementation section in the .m file.
The part I am not clear about is:
Can you advise me how to implement my array in to this statement so I can use it in other views.
Many thanks for any advice and help you can provide.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNext, you should not use "extern" in the .m file. "extern" is only for the declaration in the .h file, not for the actual variable definition in the .m file.
Finally, you already created countries and it is retained (because you used alloc and did not release it).
So if you take away that one unnecessary extern, then you would have the setup you need to make countries a global NSMutableArray. To access it in other views, only need to import the .h file that declares countries into the .m file of those views.
I should warn you that using a global variable like this invites problems if you are careless. Remember that countries is not actually the NSMutableArray itself. It is just a pointer to that NSMutableArray. Like all Objective-C objects, this object can only be accessed through pointers. And the memory that it points to needs to be dynamically managed. Normally this dynamic memory management is taken care of for you when you make a property of an object.
By looking at your code, I see that you set countries in response to a didSelectRowAtIndexPath. Therefore you might set countries more than once. When you do that, the old NSMutableArray that countries used to point to becomes leaked memory, unless you first release it. This would have been handled automatically for you if you were content to use properties and the standard retaining setter that comes with it. But by implementing a global variable, you have abandoned the use of properties. So you have to do the work that the properties would have done for you automatically. If you really understand the retain/release system and are committed to taking the care needed to release dynamically allocated memory at just the right time, then you can make this work. But this is not normally done.
OK, I have done my best to warn you against using a global variable as a pointer to a NSMutableArray. So the choice is up to you.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeMany thanks for such a details response. After reading tier reply I think I need to look at how I am going the get the result I need in another way.
Because "countries" will be set more than once your right in saying it's not the way to do it. The only reason I was looking at using a global array is because I cannot think of any other way to retain the variables in each of the views.
I need to rethink the whole process.
Again, many thanks for your advice.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeFrom what I have read, "and since I started trying to learn Objective C, which was back in March, I've done a lot of reading but not enough by the look of it" your suggestion is a great idea. The only problem I have is I am not sure how to implement it.
Do you have the time to show me the syntax I should use to make the "countries" array a property of the App delegate and how I would make a call to it from a UIView.
I think if I can get over this issue I may be home and dry.
Again, many thanks in advance.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeappDelegate.countries =...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSorry to be a complete pain in the neck with this.
My appDelegate .h file is:
The appDelegate .m file is:
and so on...
I have a file which reads my SQL data and creates an ARRAY containing the record requested by the user. A part of the file is below:
The part I am struggling with is assigning the content of the ARRAY to the "appDelegate.countries = countries". I know this is because I don't know how this is done even thou I have read and re-read your tutorial. Basically I am having a very thick head time.
Can you help me advance this issue a little further.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeagain, sorry to be a pain in the neck.
I have gotten a little further, I now have the following in the file which reads the country record in to the array. I have changed he name of the array from countries to countriesList to avoid a clash with another var.
The App complies without error but the output of the NSLog is:
2010-07-31 20:47:52.163 iTravelv113[2983:207] Database Successfully Opened
2010-07-31 20:47:52.165 iTravelv113[2983:207] The contents of countriesList = (
""
)
I am not to sure what this is telling me. Can you throw any light on the output.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomePlease forget the last post or two. I am moving forward and I think I may have solved my issue.
I will let you know, many thanks again for your help.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHaving followed your tutorial I now have the following code.
I have created an NUMultableArray:
which when test for content holds the correct data.
In my AppDeligate I have the following:
.h file:
in the .m file:
In the UIViewController where I want to use the content of the array I have:
.h file:
.m file
but I am getting the following error:
"Accessing unknown setAcountryName class method, Object cannot be set, either readonly property or setter cannot be found".
Can you see anything I have missed.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeacountryName is not a property of UITextView. Why is UITextView there?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAssuming all the other code is correct, How do I call the content of the "countries" array so it can be used the the view I am working on.
I know I'm a pain in the neck, sorry. But I am learning fast, I think.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSUCCESS, I have change my code to:
Now I have the correct data displaying.
Robert, many thank for all your help.
Dereck
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome