Using a string as name to set property values - ios

I have these big huge repetitive chunks of code in my project I am attempting to shrink them down. Take this piece example:
self.Day11.delegate = (id)self;
self.Day12.delegate = (id)self;
self.Day13.delegate = (id)self;
self.Day14.delegate = (id)self;
self.Day15.delegate = (id)self;
self.Day16.delegate = (id)self;
self.Day17.delegate = (id)self;
self.Day18.delegate = (id)self;
self.Day19.delegate = (id)self;
What I would like to do is make it that I can use a for loop or something similar to shrink it down like this:
for (int i = 1 ; i<=9; i++) {
NSString *var = [NSString stringWithFormat:#"Day1%d",i];
self.var.delegate = (id)self;
}
I know this doesn't work is there a possible way to do something like this?

No, no, no.
#property (nonatomic,strong) NSArray *arrayOfDays;
Now get rid of all those day objects and fill that self.arrayOfDays with whatever all those individual day objects are...
Then...
for(int i=0; i<[self.arrayOfDays count]; ++i) {
[[self.arrayOfDays objectAtIndex:i] setDelegate: self];
}
Or even better, if all those objects are of the same type (I'll assume they're of type Day), we can do:
for(Day *day in self.arrayOfDays) {
day.delegate = self;
}
Best (per Daij-Dan's comment):
[self.arrayOfDays setValue:self forKeyPath:#"delegate"];

Related

Is it possible to increase destination point radius? [duplicate]

This is my code for creating trackable POIs and starting a POI tracking.
This code is placed just before I would calculate a route and start navigation
-(void)createTrackablePOIs
{
// Go through the route array and create POIs out of all added exits and locations
if ([Constants shared].routeArray.count>2){
_trackablePOIs = [[NSMutableArray alloc] init];
for (int i=1; i<[Constants shared].routeArray.count-1; i++) {
if ([[Constants shared].routeArray[i] isKindOfClass:[Exit class]]){
Exit *e = [Constants shared].routeArray[i];
SKTrackablePOI* poi = [[SKTrackablePOI alloc] init];
poi.poiID = e.idExit;
poi.type = 1;
poi.coordinate = CLLocationCoordinate2DMake(e.lat, e.lon);
[self.trackablePOIs addObject:poi];
}else{
RoutePoint* rp = [Constants shared].routeArray[i];
SKTrackablePOI *poi = [[SKTrackablePOI alloc] init];
poi.poiID = rp.pointID;
poi.type = 2;
poi.coordinate = CLLocationCoordinate2DMake(rp.x,rp.y);
[self.trackablePOIs addObject:poi];
}
}
NSLog(#"%d POIs created and added to array",_trackablePOIs.count);
self.poiTracker = [[SKPOITracker alloc] init];
self.poiTracker.dataSource = self;
self.poiTracker.delegate = self;
// Create POI Tracking rules
SKTrackablePOIRule *rule = [SKTrackablePOIRule trackablePOIRule];
rule.routeDistance = 5000;
rule.aerialDistance = 5000;
[_poiTracker setRule:rule forPOIType:1];
// Start POI tracking
[_poiTracker startPOITrackerWithRadius:5000 refreshMargin:0.1 forPOITypes:#[#1,#2]];
}
}
-(NSArray*)poiTracker:(SKPOITracker *)poiTracker trackablePOIsAroundLocation:(CLLocationCoordinate2D)location inRadius:(int)radius withType:(int)poiType
{
return [self.trackablePOIs copy];
}
- (void)poiTracker:(SKPOITracker *)poiTracker didDectectPOIs:(NSArray *)detectedPOIs{
[detectedPOIs enumerateObjectsUsingBlock:^(SKDetectedPOI *detectedPOI, NSUInteger index, BOOL *stop){
NSLog(#"Detected: %#",[detectedPOI description]);
}];
NSLog(#"POI detected");
}
But POI delegate method is never called. No matter how much I played with trackable POI rules.
Also, another question, what exactly does the margin stands for in
[_poiTracker startPOITrackerWithRadius:5000 refreshMargin:0.1 forPOITypes:#[#1,#2]];

How to start Tracking POIs with Skobbler

This is my code for creating trackable POIs and starting a POI tracking.
This code is placed just before I would calculate a route and start navigation
-(void)createTrackablePOIs
{
// Go through the route array and create POIs out of all added exits and locations
if ([Constants shared].routeArray.count>2){
_trackablePOIs = [[NSMutableArray alloc] init];
for (int i=1; i<[Constants shared].routeArray.count-1; i++) {
if ([[Constants shared].routeArray[i] isKindOfClass:[Exit class]]){
Exit *e = [Constants shared].routeArray[i];
SKTrackablePOI* poi = [[SKTrackablePOI alloc] init];
poi.poiID = e.idExit;
poi.type = 1;
poi.coordinate = CLLocationCoordinate2DMake(e.lat, e.lon);
[self.trackablePOIs addObject:poi];
}else{
RoutePoint* rp = [Constants shared].routeArray[i];
SKTrackablePOI *poi = [[SKTrackablePOI alloc] init];
poi.poiID = rp.pointID;
poi.type = 2;
poi.coordinate = CLLocationCoordinate2DMake(rp.x,rp.y);
[self.trackablePOIs addObject:poi];
}
}
NSLog(#"%d POIs created and added to array",_trackablePOIs.count);
self.poiTracker = [[SKPOITracker alloc] init];
self.poiTracker.dataSource = self;
self.poiTracker.delegate = self;
// Create POI Tracking rules
SKTrackablePOIRule *rule = [SKTrackablePOIRule trackablePOIRule];
rule.routeDistance = 5000;
rule.aerialDistance = 5000;
[_poiTracker setRule:rule forPOIType:1];
// Start POI tracking
[_poiTracker startPOITrackerWithRadius:5000 refreshMargin:0.1 forPOITypes:#[#1,#2]];
}
}
-(NSArray*)poiTracker:(SKPOITracker *)poiTracker trackablePOIsAroundLocation:(CLLocationCoordinate2D)location inRadius:(int)radius withType:(int)poiType
{
return [self.trackablePOIs copy];
}
- (void)poiTracker:(SKPOITracker *)poiTracker didDectectPOIs:(NSArray *)detectedPOIs{
[detectedPOIs enumerateObjectsUsingBlock:^(SKDetectedPOI *detectedPOI, NSUInteger index, BOOL *stop){
NSLog(#"Detected: %#",[detectedPOI description]);
}];
NSLog(#"POI detected");
}
But POI delegate method is never called. No matter how much I played with trackable POI rules.
Also, another question, what exactly does the margin stands for in
[_poiTracker startPOITrackerWithRadius:5000 refreshMargin:0.1 forPOITypes:#[#1,#2]];

UITextfield text in array will not change a label's text in another array

Please help. What am I doing wrong? textfield.text goes into one array and is suppose to change a label that belongs to another array.
I have multiple textfields. Im trying to save the text of each field to an array and then setAlpha to 0. Then I have an equal amount of labels that I want to change to the textfield's text. I have tried using mutable and immutable arrays. I keep getting errors.
I've tried multiple ways and its got to be something simply dumb I'm missing. I've greatly reduced these arrays just to post here.
_namesText = [NSMutableArray arrayWithObjects:_nameLabel1.text, _nameLabel2.text, nil];
_names = [NSMutableArray arrayWithObjects:_nameLabel1, _nameLabel2, nil];
_nameInputs = [NSMutableArray arrayWithObjects:_p1NameTextField, _p2NameTextField, nil];
_playerNameText = [NSArray arrayWithObjects:_p1NameTextField.text, _p2NameTextField.text, nil];
enter code here
- (IBAction)enterNamesButton:(id)sender {
//These don't work.
for (int i = 0; i < _numberOfPlayers; i++) {
[_names[i] setAlpha:1];
NSString *tempString = [[NSString alloc]initWithString:[_nameInputs[i] text]];
[_lastTry addObject:tempString];
}
//Then tried this. This is after trying for 2.5 hours and different coding.
for (int i = 0; i < _numberOfPlayers; i++) {
_namesText[i] = _lastTry[i];
UILabel *cell = [[UILabel alloc] init];
cell.text = _lastTry[i];
//[[_namesText[i] textLabel] text] = #"Idiot";
_namesText[i] = cell;
NSLog(#"%#", _namesText[i]);
// This works (but bad practice) and I want to loop through arrays that each UI element belongs to instead of typing it all out.
// _nameLabel1.text = _p1NameTextField.text;
// _nameLabel2.text = _p2NameTextField.text;
I expect this to work but NOPE!!!!
Assuming the following:
Array _fromTextField is the textfield you want to copy from and set Alpha to 0
Array _toLabelField is the label you want the text to appear in
Array _toStoreStrings is where you keep all the strings, for later maybe ?
NSMutableArray *_fromTextField = [[NSMutableArray alloc] init]; // Put the Text Fields in Here
NSMutableArray *_toLabelField = [[NSMutableArray alloc] init]; // Put the Labels in Here
NSMutableArray *_toStoreStrings = [[NSMutableArray alloc] init];
int count = 0;
int overRun = [_toLabelField count];
for (UITextField *tf in _fromTextField)
{
tf.alpha = 0;
NSString *tempString = [[NSString alloc]initWithString:tf.text];
[_toStoreStrings addObject:tempString];
if (count < overRun) // Check just in case Array sizes not same
{
UILabel *lb = [_toLabelField ObjectAtIndex:count];
lb.text = tempString;
}
else
break;
count++;
}
The issue your code faces is you have an array of NSObject. The compiler has no idea they are UITextField or UILabel unless you cast them with (type_cast *) or point a new typed variable at them. Hence the crashes you were seeing.
I would do it this way:
for (int i = 0; i < _numberOfPlayers; i++) {
_namesText[i] = _lastTry[i];
}
I cannot see why this won't work... does it?

IOS library to convert powerpoint presentations into images

after a lot of searching and still searching, i'm unable to find the way to extract the slides out of powerpoint presentations. Need help, if they know anything related this or have implemented such thing, source code will be great or any reference link will also work. I have been using QLPreviewController, but i failed to override it.
I am not sure whether this is still required. but anyways, this is how I have implemented
(1) Use UIWebView to display the content of the PPT. The well formed (!) HTML allows you to parse it. Get the html using
NSString *htmlContent = [self.webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
(2) Count the number of div class = 'slide to get the slide count
(4) You need to parse the HTML to get the styles and div using the below code
(5) Get all the styles
- (NSString *)getStyles:(int)slideCount forView:(UIWebView *)view {
NSString *getElementByTag = [[NSString alloc] init];
NSString *allStyles = [[NSString alloc] init];
for (int i = 0; i < slideCount; i++) {
getElementByTag = [NSString stringWithFormat:#"document.getElementsByTagName('style')[%d].innerHTML", i];
NSString *style = [[view stringByEvaluatingJavaScriptFromString:getElementByTag] stringByAppendingString:#"\n"];
allStyles = [allStyles stringByAppendingString:style];
}
allStyles = [#"<style type='text/css'>" stringByAppendingString: allStyles];
return [allStyles stringByAppendingString:#"</style>"];
}
(6) Concatenate all the styles from step 5 with div tag content. Define a NSMutableArray variable to to store all slides with styles for future reference. Define NSMutableArray *slides to store all the slides
- (void)makeSlides:(int)slideCount forView:(UIWebView *)view {
self.slides = [[NSMutableArray alloc] init];
for (int i = 0; i < slideCount; i++) {
NSString *slideBody = [NSString stringWithFormat:#"document.getElementsByClassName('slide')[%d].innerHTML", i];
NSString *div = [view stringByEvaluatingJavaScriptFromString:slideBody];
NSString *slide = [self.slideStyles stringByAppendingString:div];
[self.slides addObject:slide];
}
}
Hope this helps

NSDictionary loses data?

I've seen posts where NSDictionary loses data but in my case its a little wierd. I have a class that includes some data including NSString and NSIntegers and GLfloats. (As far as I know all of these do conform to NSCopying unlike things like CGFloat.)
I access the files quickly in my app and it all works, but then I try to access it again (to reload/refresh) the screen and then the NSInteger values return something between 180000, and a few billion for a value I know is definitely 4 so for some reason the data hasn't persisted.
It feels like the data is being lost/released/changed at some point but I don't know how it possibly can.
Edit:
Heres some of the code, this is where the texturedQuads are created and store, the method is called for each atlas I have in the init method
Also I have changed where I misused the word wrapper
-(void)loadAtlasData:(NSString *)atlasName
{
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
if (quadLibrary == nil)
quadLibrary = [[NSMutableDictionary alloc] init];
CGSize atlasSize = [self loadTextureImage:[atlasName
stringByAppendingPathExtension:#"png"]
materialKey:atlasName];
NSArray *itemData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:atlasName ofType:#"plist"]];
for(NSDictionary *record in itemData)
{
TexturedQuad *quad = [self texturedQuadFromAtlasRecord:record
atlasSize:atlasSize
materialKey:atlasName];
[quadLibrary setObject:quad forKey:
[record objectForKey:#"name"]];
}
[apool release];
}
-(TexturedQuad *)texturedQuadFromAtlasRecord:(NSDictionary *)record
atlasSize:(CGSize)atlasSize
materialKey:(NSString *)key
{
TexturedQuad *quad = [[TexturedQuad alloc] init];
GLfloat xLocation = [[record objectForKey:#"xLocation"] floatValue];
GLfloat yLocation = [[record objectForKey:#"yLocation"] floatValue];
GLfloat width = [[record objectForKey:#"width"] floatValue];
GLfloat height = [[record objectForKey:#"height"] floatValue];
//find the normalized texture co-ordinates
GLfloat uMin = xLocation/atlasSize.width;
GLfloat vMin = yLocation/atlasSize.height;
GLfloat uMax = (xLocation + width)/atlasSize.width;
GLfloat vMax = (yLocation + height)/atlasSize.height;
quad.uvCoordinates[0] = uMin;
quad.uvCoordinates[1] = vMax;
quad.uvCoordinates[2] = uMax;
quad.uvCoordinates[3] = vMax;
quad.uvCoordinates[4] = uMin;
quad.uvCoordinates[5] = vMin;
quad.uvCoordinates[6] = uMax;
quad.uvCoordinates[7] = vMin;
quad.materialKey = key;
return [quad autorelease];
}
Second edit:
Added an example of the plist file
dict
(key)name
(string)sync
(key)xLocation
(integer)489
(key)yLocation
(integer)36
(key)width
(integer)21
(key)height
(integer)21
/dict
essentially its an array, consisting of dictionaries, each dictionary holds the data for the picture in the atlas. so theres the name, xLocation, yLocation, width, height.
edit 3:
Here is where I load the object from
I use a [MaterialController sharedMaterialController] to get an instance of this controller
-(TexturedQuad *)quadFromAtlasKey:(NSString *)atlasKey
{
return [quadLibrary objectForKey:atlasKey];
}
See if removing the auto-release pool sorts the issue. As far as I can see you don't need it as quadLibrary seems to be an iVar whilst itemData is auto-released by its parent class, as are all the TextureQuads you return in your for loop.

Resources