ios - Weird plist behaviour returning null on first value and 13 results when it should be 12 -
i have strange behaviour when using plist , trying return data. give overview plist has 12 entries in each object expect 12 instead 13 first result being null while remaining 12 in fact correct.
here plist file:
<?xml version="1.0" encoding="utf-8"?> <!doctype plist public "-//apple//dtd plist 1.0//en" "http://www.apple.com/dtds/propertylist-1.0.dtd"> <plist version="1.0"> <dict> <key>name</key> <array> <string>audit</string> <string>tax</string> <string>digital</string> <string>consulting</string> <string>risk advisory</string> <string>finance</string> <string>audit</string> <string>tax</string> <string>digital</string> <string>consulting</string> <string>advisory</string> <string>finance</string> </array> <key>logo</key> <array> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> <string>page.png</string> </array> </dict> </plist> this viewcontroller.h file:
#import <uikit/uikit.h> #import "icarousel.h" @interface packviewcontroller : uiviewcontroller <icarouseldatasource, icarouseldelegate> { nsmutablearray *packname; nsmutablearray *packlogo; } @property (nonatomic, retain) iboutlet icarousel *carousel; @property (nonatomic, retain) nsmutablearray *packname; @property (nonatomic, retain) nsmutablearray *packlogo; @end and then, viewcontroller.m file:
#import "packviewcontroller.h" #import "asyncimageview.h" @interface packviewcontroller () <uiactionsheetdelegate> @property (nonatomic, assign) bool wrap; @property (nonatomic, retain) nsmutablearray *items; @end @implementation packviewcontroller @synthesize carousel, wrap, items; @synthesize packname, packlogo; - (void)awakefromnib { //set data wrap = yes; nsstring *plistpath = [[nsbundle mainbundle] pathforresource:@"pack" oftype:@"plist"]; nsarray *plistitems = [nsdictionary dictionarywithcontentsoffile:plistpath]; nsarray *plistdata = [plistitems valueforkey:@"name"]; self.items = [nsmutablearray array]; (int = 0; < [plistdata count]; i++) { [items addobject:[nsnumber numberwithint:i]]; nslog(@"first index name %@",[items objectatindex:i]); } } - (void)dealloc { carousel.delegate = nil; carousel.datasource = nil; [carousel release]; [super dealloc]; } #pragma mark - #pragma mark view lifecycle - (void)viewdidload { [super viewdidload]; // pack.plist code // paths root direcory nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); // documents path nsstring *documentspath = [paths objectatindex:0]; // path our pack.plist file nsstring *plistpath = [documentspath stringbyappendingpathcomponent:@"pack.plist"]; // check see if pack.plist exists in documents if (![[nsfilemanager defaultmanager] fileexistsatpath:plistpath]) { // if not in documents, property list main bundle plistpath = [[nsbundle mainbundle] pathforresource:@"pack" oftype:@"plist"]; } // read property list memory nsdata object nsdata *plistxml = [[nsfilemanager defaultmanager] contentsatpath:plistpath]; nsstring *errordesc = nil; nspropertylistformat format; // convert static property list dictionary object nsdictionary *temp = (nsdictionary *)[nspropertylistserialization propertylistfromdata:plistxml mutabilityoption:nspropertylistmutablecontainersandleaves format:&format errordescription:&errordesc]; if (!temp) { nslog(@"error reading plist: %@, format: %d", errordesc, format); } // assign values self.packname = [nsmutablearray arraywitharray:[temp objectforkey:@"name"]]; self.packlogo = [nsmutablearray arraywitharray:[temp objectforkey:@"logo"]]; // configure carousel carousel.type = icarouseltypeinvertedwheel; carousel.vertical = true; } - (void)viewdidunload { [super viewdidunload]; self.carousel = nil; } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { return yes; } #pragma mark - #pragma mark uiactionsheet methods - (void)actionsheet:(uiactionsheet *)actionsheet diddismisswithbuttonindex:(nsinteger)buttonindex { if (buttonindex >= 0) { //map button index carousel type icarouseltype type = buttonindex; //carousel can smoothly animate between types [uiview beginanimations:nil context:nil]; carousel.type = type; [uiview commitanimations]; } } #pragma mark - #pragma mark icarousel methods - (nsuinteger)numberofitemsincarousel:(icarousel *)carousel { //nslog(@"numberofitemsincarousel count: %i", [items count]); return [items count]; } - (uiview *)carousel:(icarousel *)carousel viewforitematindex:(nsuinteger)index reusingview:(uiview *)view { uibutton *button = (uibutton *)view; uilabel *label = nil; if (button == nil) { //no button available recycle, create new 1 uiimage *image = [uiimage imagenamed:[nsstring stringwithformat:@"%@", [packlogo objectatindex:index]]]; //nslog(@"companylogo: %@", [packlogo objectatindex:index]); button = [uibutton buttonwithtype:uibuttontypecustom]; button.frame = cgrectmake(0.0f, 0.0f, image.size.width, image.size.height); [button settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal]; [button setbackgroundimage:image forstate:uicontrolstatenormal]; button.titlelabel.font = [button.titlelabel.font fontwithsize:20]; [button addtarget:self action:@selector(buttontapped:) forcontrolevents:uicontroleventtouchupinside]; label = [[[uilabel alloc] initwithframe:view.bounds] autorelease]; label.backgroundcolor = [uicolor clearcolor]; label.textalignment = uitextalignmentcenter; } else { //get reference label in recycled view label = (uilabel *)[view viewwithtag:1]; } //set button label [button settitle:[self.packname objectatindex:index] forstate:uicontrolstatenormal]; nslog(@"companyname: %@", [items objectatindex:index]); return button; } #pragma mark - #pragma mark item tap event - (void)buttontapped:(uibutton *)sender { //get item index button nsinteger index = [carousel indexofitemvieworsubview:sender]; [[[[uialertview alloc] initwithtitle:@"button tapped" message:[nsstring stringwithformat:@"you tapped button number %i", index] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil] autorelease] show]; } - (catransform3d)carousel:(icarousel *)_carousel itemtransformforoffset:(cgfloat)offset basetransform:(catransform3d)transform { cgfloat count = _carousel.numberofvisibleitems; cgfloat spacing = 1.0f; cgfloat arc = m_pi * 2.0f; cgfloat radius = _carousel.itemwidth * spacing * count / arc; cgfloat angle = arc / count * offset; return catransform3dtranslate(transform, radius - radius * cos(angle), radius * sin(angle), 0.0f); } - (cgfloat)carousel:(icarousel *)_carousel valueforoption:(icarouseloption)option withdefault:(cgfloat)value { //customise carousel display switch (option) { case icarouseloptionwrap: { //normally hard-code yes or no return wrap; } case icarouseloptionspacing: { //add bit of spacing between item views return value * 1.05f; } case icarouseloptionfademax: { if (carousel.type == icarouseltypecustom) { //set opacity based on distance camera return 0.0f; } return value; } default: { return value; } } } @end you can see have 2 lines of code in m file outputs 2 nslog items. these are:
nslog(@"first index name %@",[items objectatindex:i]); nslog(@"companyname: %@", [items objectatindex:index]); the output "first index name" contained within loop in awakefromnib is:
2013-05-21 22:43:23.875 pack[7496:c07] first index name 0 2013-05-21 22:43:23.877 pack[7496:c07] first index name 1 2013-05-21 22:43:23.878 pack[7496:c07] first index name 2 2013-05-21 22:43:23.879 pack[7496:c07] first index name 3 2013-05-21 22:43:23.880 pack[7496:c07] first index name 4 2013-05-21 22:43:23.880 pack[7496:c07] first index name 5 2013-05-21 22:43:23.881 pack[7496:c07] first index name 6 2013-05-21 22:43:23.881 pack[7496:c07] first index name 7 2013-05-21 22:43:23.882 pack[7496:c07] first index name 8 2013-05-21 22:43:23.882 pack[7496:c07] first index name 9 2013-05-21 22:43:23.883 pack[7496:c07] first index name 10 2013-05-21 22:43:23.883 pack[7496:c07] first index name 11 as can see i'm getting 12 values in correct ordering kind of expecting second 1 problem come in. second nslog contained in -(uiview *)carousel:(icarousel *)carousel viewforitematindex:(nsuinteger)index reusingview:(uiview *)view i'm outputting items array see happening. see:
2013-05-21 22:43:23.916 pack[7496:c07] companyname: 0 2013-05-21 22:43:23.918 pack[7496:c07] companyname: 0 2013-05-21 22:43:23.919 pack[7496:c07] companyname: 11 2013-05-21 22:43:23.920 pack[7496:c07] companyname: 7 2013-05-21 22:43:23.921 pack[7496:c07] companyname: 6 2013-05-21 22:43:23.922 pack[7496:c07] companyname: 2 2013-05-21 22:43:23.922 pack[7496:c07] companyname: 1 2013-05-21 22:43:23.923 pack[7496:c07] companyname: 10 2013-05-21 22:43:23.924 pack[7496:c07] companyname: 9 2013-05-21 22:43:23.925 pack[7496:c07] companyname: 8 2013-05-21 22:43:23.925 pack[7496:c07] companyname: 5 2013-05-21 22:43:23.926 pack[7496:c07] companyname: 4 2013-05-21 22:43:23.927 pack[7496:c07] companyname: 3 again first 2 results both 0 , ordering quite messed unlike first iteration. found thread on so mentions plists don't output in order key objects core issue first 2 results.
if change nslog output results of self.packname (like this: nslog(@"companyname: %@", [self.packname objectatindex:index]);) here returned:
2013-05-21 22:49:43.663 pack[7543:c07] companyname: (null) 2013-05-21 22:49:43.665 pack[7543:c07] companyname: audit 2013-05-21 22:49:43.666 pack[7543:c07] companyname: finance 2013-05-21 22:49:43.667 pack[7543:c07] companyname: tax 2013-05-21 22:49:43.668 pack[7543:c07] companyname: audit 2013-05-21 22:49:43.669 pack[7543:c07] companyname: digital 2013-05-21 22:49:43.669 pack[7543:c07] companyname: tax 2013-05-21 22:49:43.670 pack[7543:c07] companyname: risk advisory 2013-05-21 22:49:43.671 pack[7543:c07] companyname: consulting 2013-05-21 22:49:43.672 pack[7543:c07] companyname: digital 2013-05-21 22:49:43.672 pack[7543:c07] companyname: finance 2013-05-21 22:49:43.673 pack[7543:c07] companyname: risk advisory 2013-05-21 22:49:43.673 pack[7543:c07] companyname: consulting as can see again, first result (null) , i'm stumped why happening.
to further debug ran nslog: nslog(@"companyname: %@", packname); output raw results array , these results:
2013-05-21 22:52:59.284 pack[7581:c07] companyname: ( "audit", "tax", "digital", "consulting", "risk advisory", "finance", "audit", "tax", "digital", "consulting", "advisory", "finance" ) there's no (null) result in array don't have clue rogue null value coming from.
any immensely appreciated!!!!
for second nslog contained in
carousel:viewforitematindex:reusingview:i'm outputting items array see happening.
no, outputting 1 element of items: 1 index passed in icarousel delegate callback. have no control on when icarousel sends message , order of indices not specified. explains following output
2013-05-21 22:43:23.916 pack[7496:c07] companyname: 0 2013-05-21 22:43:23.918 pack[7496:c07] companyname: 0 2013-05-21 22:43:23.919 pack[7496:c07] companyname: 11 ... again first 2 results both 0 , ordering quite messed unlike first iteration. found thread on mentions plists don't output in order key objects core issue first 2 results.
as explained order depends on icarousel implementation. has nothing plists. misread linked post, it's order in dictionaries. of course, plists preserve array element order.
if change nslog output results of self.packname (like this: nslog(@"companyname: %@", [self.packname objectatindex:index]);) here returned:
2013-05-21 22:49:43.663 pack[7543:c07] companyname: (null) 2013-05-21 22:49:43.665 pack[7543:c07] companyname: audit ... the line (null) result can mean 1 thing: @ first callback items ivar not yet initialized. objectatindex: can never return nil, explanation. check using debugger, setting breakpoints in callback , awakefromnib.
Comments
Post a Comment