hello there,
working on project third year of computer science, in have build app ipad primary school children welsh.
have dictionary contains png images, of weather in case, string relates image.
trying make whenever view loaded menu, images shown on screen @ random. hard code specific images appear on specific uiimageview result in images being displayed in same place every time.
friend has helped me following code doesn't want do.
any aid appreciated. wasn't sure else need know give me shot if need more informationcode:nsstring *filename = [[nsbundle mainbundle] pathforresource:@"weather plist" oftype:@"plist"]; weatherdictionary = [[nsmutabledictionary alloc] initwithcontentsoffile:filename]; recordindex = arc4random()%[weatherdictionary count]; image1.image = [uiimage imagenamed:[[weatherdictionary valueforkey:@"cloudy"] valueforkey:@"image"]]; //image2 //image3 //image4
edit: i'm aware title doesn't make sense. apologise in advance if find unclear/misleading in anyway.
start analyzing current code trying do.
first, load dictionary plist file. apparently contains image-names values, other names "cloudy" keys.
second, produce random number based on dictionary count. or try to. let's analyze this. first, dictionary keys , values don't have numbered position, array does. generating random number identifying position isn't going useful. in other words, if generate random number between 0 , dictionary's item count, can't directly use number retrieve nth dictionary item.
can do, however, keys nsarray, use random number index that. nsdictionary class reference, , find method that.
next, have shuffle array randomize it. pick random numbers , remove keys array, view have images, you'd have no way of knowing key image belonged to. preserve correlation between on-screen image views , current randomized arrangement of keys, keep shuffled nsarray. fisher-yates shuffle simple , effective shuffling algorithm.
@ point, have shuffled array of keys. assign image view, key sequential position n of array, use image name dictionary, load , assign image view object n.
solution came breaking things down smaller pieces, , solving each piece. need understand piece @ each step. example, recognizing random number useless nsdictionary, because dictionaries don't have numbered entries, nsarrays do.
above assumes have multiple image-views on-screen @ once, , want them randomly assigned views. it's unclear original post whether mean this, or whether mean have 1 image visible @ time, , user views them sequentially, sequence randomized. either way, same basic approach work: keys array, shuffle array, take sequential keys array.
Forums iPhone, iPad, and iPod Touch iOS Programming
- iPhone
- Mac OS & System Software
- iPad
- Apple Watch
- Notebooks
- iTunes
- Apple ID
- iCloud
- Desktop Computers
- Apple Music
- Professional Applications
- iPod
- iWork
- Apple TV
- iLife
- Wireless

No comments:
Post a Comment