objective c - Selecting random String from .plist? -
i new developing , have run problem. read string .plist , when button pressed picks random string , presents in label? have sample .plist , button instantiated in .h , .m don't know how select random string , change uilabels value selected string. appreciated, , in advance!.
heres .plist
and here .h
#import <uikit/uikit.h> @interface viewcontroller2 : uiviewcontroller { iboutlet uilabel *label1; } -(ibaction)randombutton;
and here .m
#import "viewcontroller2.h" @interface viewcontroller2 () @end @implementation viewcontroller2 //what put in randombutton method extract .plist? -(ibaction)randombutton { }
first of should rearrange plist file, strings in 1 array (now strings not in "words" array). if so, read plist nsarray:
nsstring *path = [[nsbundle mainbundle] pathforresource: @"my" oftype:@"plist"]; nsdictionary *plistdict = [[nsdictionary alloc] initwithcontentsoffile:path]; nsmutablearray *plistarray = plistdict[@"words"];
then, generate random variable:
int randv = arc4random() % plistarray.count; // randv 0 number of strings -1 in array
then, set label's text:
label1.text = plistarray[randv];
also, highly recommend read books or go through few tutorials before asking questions that.
Comments
Post a Comment