I have one array.
I want that array to retain its value between function calls of from single class function.
I have function that is called every time page is loaded.
This function displays all the data stored in array right from application launches.
Thanks in advance.
You can store your array in appDelegate class. Or I think it will be better to make some class to store shared data and make an instance of this class in your appDelegate.
Related
I usually use a segue to pass some data between 2 ViewControllers. But now I have a 2D array (About 40Meg of data and in the future it may go to over 100Meg. It's a .json file). I read a few articles that tell all the different ways. But most ways (if not all) will make a copy of that data, and that takes time (in the launch screen it's acceptable but not when switching of ViewControllers). What method would you recommend? Putting my huge array as a global works fine, but it is frowned by many.
class ArrayWapper {
var array: [[Int]] = [[]] // Set your array .
}
And send an instance from that class to the second UIViewController
as, #vacawama mentioned since its class it will pass the reference rather than a value copy of that array.
So in the second UIViewController you would have a variable of ArrayWapper class type, instead of simply [[Int]] array.
while going through the class reference file for NSUserDefaults, I notice that the return value for [NSUserDefaults standardUserDefaults]is
The shared defaults object
what is this object, what is its type? which methods we can call on this object?
Thanks,
It's an instance of NSUserDefaults. You can call any of the instance methods that are defined for that class. (e.g. stringForKey:, dictionaryForKey:....)
+ (NSUserDefaults *)standardUserDefaults
It is just class method for returning singleton object of type NSUserDefaults. So, you can invoke all methods for this class.
From NSUserDefaults Class Reference
The defaults are initialized for the current user. Subsequent modifications to the standard search list remain in effect even when this method is invoked again—the search list is guaranteed to be standard only the first time this method is invoked. The shared instance is provided as a convenience—you can create custom instances using alloc along with initWithUser: or init.
I want to know how i can declare and use a global NSMutable array of an Objective C project.
I mean i need to update the here in one class and need to use in another class....
can anyone give me any kind of idea??
Thanks
Rony
Pass a reference around from class to class. Or keep a reference to the array instance in your app delegate, which you can access from any class in your application.
You could use the singleton pattern and put the array into the singleton.
i am a new iphone programmer i want to create an array that keeps adding a value at its last position when i moving from one view to another
because at last view ,i need that full array which has all values from all views....
i tried taking an array and sending it view to next view (array is defined in all views) but when i tried to put value to it
You can use NSMutableArray class with addObject: in order to add your views into array. Have a single view controller, declare that array and all your views can access the view controller's array.
try to save it to a class (singelton class) it will work....it will surely work if you transfer it to the delegate class....make an NSMutableArray object there and initialize it once in applicationDidFinishLaunching method and then make object of delegate class and transfer your values to it by addObject method.....
The best way is initialize your mutablearray in appdelegate and use the same using the object of delegate in each class wherever you want to use.you can add the object using addobject and can access using property objectAtIndex anywhere.
I understand that in Objective-C you declare an array in the header file and interact with it in a class. So far I'm adding things and fetching them fine within a single function. I'm new to the language however and can't figure out how to share that array across other functions.
I'd like to initialize array data in my viewDidLoad and access it from various functions later on. Is this possible and if so what's the best way to do it?
Like you said, declare the array in the view controller's header file and make it a #property. Use alloc-init in the implementation's -viewDidLoad method to set it up. Deallocate it in the dealloc method. Use its property setter (self.array) to retain or assign another array, depending on the #property attribute. Access it directly (array) throughout your methods in your class implementation, and via its property getter (obj.array) from other classes.