I have created an NSArray
from a text file with
void arrayfromfile {
// get a reference to our file
NSString *myPath = [[NSBundle mainBundle]pathForResource:@"CalendarQuotes" ofType:@"txt"];
// read the contents into a string
NSString *myFile = [[NSString alloc]initWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:nil];
// split our string into an array
NSArray *mySplit = [myFile componentsSeparatedByString:@"\n"];}
Now what I'd like to do is set label.text
to mySplit[x] while x< mysplit.count
and call it every time a user swipes up.
setlabeltextmethod quote {
if x<mySplit.count {
label.text = mySplit objectatindex x
x+=1
else { label.text = @"thats it folks" }
}
What I'm having trouble with is accessing mySplit
from the quote so I can manipulate it to give me the quote at index x.
When I tried to access it from my swipe method:
- (IBAction)swipeUp:(id)sender {
[self doAnimationUp];
NSLog (@"swipedUp");
NSLog(@"array%@" , [mySplit objectAtIndex:0]);
NSLog(@"arraycreated");
//I'm using NSLog for debug purposes but this is where I would setlabeltext.text to mySplit objectAtIndex
}
I get array[null]
.
Your mySplit
variable is local to arrayfromfile
, so once that method returns the variable has gone. There is more than one approach to solving this. One is making it an instance variable, that is a variable that belongs to your current class instance and is visible to every instance method. Start you implementation like this:
@implementation MyClassName
{
NSArray *mySplit;
}
Now set the variable in the instance method arrayfromfile
and read it in the instance method swipeUp
- both calls must be made in the same instance of your class, each instance has its own set of instance variables.
Other options include adding a property to the class or, if you must, a global variable of some kind.
HTH
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.