Typecasting array object - iphone

I struct at a point in my application.I have an array object called time and a global int variable as index..so when I need to use both I need to type cast array object to integer value.How can I do this?Please help..
NSArray *time=[NSArray arrayWithObjects:#"1.08",#"1.12",#"1.14",#"1.18",#"1.20",#"1.24",#"1.25",#"1.29",#"1.30",#"1.34",#"1.45",#"1.50",#"1.51",#"1.55",#"1.56",#"2.00",#"2.01",#"2.06",#"2.07",#"2.11",#"2.12",#"2.16",#"2.17",#"2.21",nil];
index = 0;
[self performSelector:#selector(startAnimation) withObject:self afterDelay:[time objectAtIndex:index]];
This is my code,but it is giving error and telling to typecast

Try [[time objectAtIndex:index] floatValue]. The objects in array are strings and you can get the float value by calling [myStr floatValue]. You can also use doubleValue here.

Related

Syntax for Extracting CGFloat out of NSDictionary

Just wondering what the syntax would be to extract a CGFloat out of an NSDictionary like follows:
slider.minimumValue = [filterAttributes valueForKey:kCIAttributeSliderMin];
An NSDictionary only holds objects. What kind of object would wrap a primitive like CGFloat? NSNumber would make sense. Now, since CGFloat is either a float or a double, you'll probably want to get the double value to preserve precision/range.
Therefore:
slider.minimumValue = [[filterAttributes valueForKey:kCIAttributeSliderMin] doubleValue];
You can only put OBJECTS into an NSDictionary (or NSARRAY). CGFloat is a literal (just maps to a float), so you can't put it into or retrieve it from the dictionary.
Instead, wrap it as an NSNumber (when you add it to the dictionary), which is an object.
NSNumber *sliderMin = [NSNumber numberWithFloat:kCIAttributeSliderMin]
Or using the new syntax, you can just say #kCIAttributeSliderMin or #(kCIAttributeSliderMin) to autobox as an NSNumber.
To get the value back out, you'll retrieve the object as an NSNumber then say, [myNumber floatVal] to get the NSFloat.
Finally, you probably want to say "objectForKey" not "valueForKey".
update - sorry, in your example you're treating kCIAttributeSliderMin as a key, and I'm using it as the 'value'; but I think you get the point. Store an NSNumber object; retrieve an NSNumber object. Sorry for any confusion swapping that may have caused.

NSInteger value not valid when passing in a variable, for custom table view class delegate method

I'm testing a custom table view style class:
HorizontalTable
It produces a horizontal table view.
One of the delegate methods equivalent to tableView:numberOfRowsInSection: is:
- (NSInteger)numberOfColumnsForTableView:(HorizontalTableView *)tableView.
If I give this a number (ex: return 10;) it is happy and it give me the number "cells" that I want. But if I feed it a value of someArray.count or an int or NSInteger variable, the table view just comes out blank, delivering no cells.
I think that the method in the custom table view class that receives the NSInteger value is this:
- (NSUInteger)numberOfPages {
NSInteger numPages = 0;
if (_delegate)
numPages = [_delegate numberOfColumnsForTableView:self];
return numPages;
}
Do I need to cast the result of someArray.count to an NSInteger?
Here you get the value from array means your array not nil so just debug and check the if condition that its come in that condition or not and what you get from NSLog
- (NSUInteger)numberOfPages {
NSInteger numPages = 0;
if (_delegate){
numPages = [_delegate numberOfColumnsForTableView:self];
NSLog(#"Total record %d",numPages);//what you get here?
}
return numPages;
}
numPages = [_delegate numberOfColumnsForTableView:self];
//self requires an object of type HorizontalTableView
NSMutableArray is editable, where as NSArray is read-only.
NSMutableArray is a subclass of NSArray and responds to messages such as addObject, removeObject and so forth; i.e. it is mutable, like the name says. Instead, NSArray is immutable, i.e. you can't add/remove objects.
In fact converting the value to be returned to the count of an NSArray, as opposed to the count an NSMutableArray fixes the issue. Why? I am not sure why an NSArray's count value is valid but not the NSMutableArray's. Anyone?

How will I be able to remove [NSNull Null] objects from NSMutableArray?

I need to remove Null object added by
[mutArrSkills addObject:[NSNull null]];
Do I need to iterate? Is there any function to remove all null values from NSMutableArray?
If need to Iterate, how will I do that?
You can use NSMutableArray's removeObjectIdenticalTo: method, as follows
[mutArrSkills removeObjectIdenticalTo:[NSNull null]];
to remove the null values. No need to iterate.
removeObjectIdenticalTo:
Removes all occurrences of a given object in the array.
Discussion
This method uses the indexOfObjectIdenticalTo: method to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined using object addresses. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).
You can try doing this,
NSNull *nullValue = [NSNull null];
[mutArrSkills removeObjectIdenticalTo:nullValue];
I hope this helps.
In Swift you first have to cast your Swift Array to NSArray, make and make it mutable so you can remove the Objective-C leftover elements, then cast it back to Array.
Fatal error: NSArray element failed to match the Swift Array Element type
// my crashing array, containing a not String element, like NSNull or anything else
let myUnsafeSwiftArray: [String]
// make it safely NSArray, then make it mutable
let mutableUnsafeArray = NSMutableArray(array: myUnsafeSwiftArray as NSArray)
// remove leftover class, like [NSNull null] aka NSNull.init()
unsafeTextures.removeObject(identicalTo: NSNull.init())
// Cast the safe array back to its supposed to by element type
let safeArray = unsafeTextures as? [String]
You may iterate like this.
for(int i=0,i<[mutArrSkills count]; i++)
{
if([[mutArrSkills objectAtIndex:i] isKindOfClass:[NSNull Class]])
{
[mutArrSkills removeObjectAtIndex:i];
}
}

How to updates array values in iphone app

I have to updates array values at position 2 and 4. How will I update array values. I am initializing my array like below.
ArrayrData=[[NSArray alloc]initWithArray:statuses]; // where statuses is also an array
You can't change the value of NSArray. So initialize your array as Mutable Array
NSMutableArray *ArrayrData=[[NSMutableArray alloc]init];
[ArrayrData addObjectsFromArray:statuses];
You can update the value in a NSMutableArray using,
– replaceObjectAtIndex:withObject:
– replaceObjectsAtIndexes:withObjects:
– replaceObjectsInRange:withObjectsFromArray:range:
– replaceObjectsInRange:withObjectsFromArray:
Use the method
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
if u want to replace them with new objects or simply access the objects at index 2 and 4 and then modify them using
[ArrayrData objectAtIndex:idx]; //idx is the index of the object you want to access, in ur case it is 2 or 4.
You can change the NSMutableArray dynamically in the application as follows:
NSMutableArray *ArrayData = [[NSMutableArray alloc]init];
[ArrayData addObjectsFromArray:statuses];
And now whenever you want to replace any object in array then you can simply replace using the "replaceObjectAtIndex" method as follows:
[ArrayData replaceObjectAtIndex:2 withObject:#"your Object or data"];
[ArrayData replaceObjectAtIndex:4 withObject:#"your Object or data"];
Let me know if you have any questions.

Int or NSInteger as object for method argument. Objective-C

I'm having some trouble passing a number as an argument for a method:
- (void)meth2:(int)next_int;
And to call that method I need this:
int next_int = 1;
[self performSelectorOnMainThread:#selector(meth2:) withObject:next_int waitUntilDone:NO];
//update next_int and call meth2 again
at this point, I get a "pointer from integer without a cast" error and would happen the same with a NSInteger. An NSNumber is not useful because it's immutable and I need to change the value constantly.
Any idea how can I do this?
Thanks.
If you're just trying to call the method, you could use the standard syntax:
[self meth2:next_int];
If you really need to use the performSelectorOnMainThread: you could wrap the number in an NSNumber for the call. You say you can't do this because you need to change the number, but you can just pull an int out and change that:
[self performSelectorOnMainThread:#selector(meth2:) withObject:[NSNumber numberWithInt:next_int] waitUntilDone:NO];
// ... later ...
- (void)meth2:(NSNumber *)number {
int myInt = [number intValue];
// do stuff with myInt
}
But maybe you mean that you want to get the value of the number as an output from your call to meth2. If that's what you mean, then you could pass in a double pointer so you can receive a new object back:
- (void)meth2:(NSNumber **)number {
int myInt = [*number intValue];
// do stuff with myInt
*number = [NSNumber numberWithInt:myInt];
}
// The caller can now operate like this:
NSNumber *number = [NSNumber numberWithInt:next_int];
[self performSelectorOnMainThread:#selector(meth2:) withObject:&number waitUntilDone:YES];
int returnInt = [*number intValue];
Of course, that's not really thread-safe, so if you're doing stuff with multiple threads, I would advise using the #synchronized keyword for access to multi-thread-accessed variables, or setting up atomic properties (i.e. properties not declared as nonatomic).
Also, meth is bad for you!! haha
Wrap the integer in an NSNumber before passing it:
int next_int = 1
NSNumber *theNumber = [NSNumber numberWithInt:next_int];
[self performSelectorOnMainThread:#selector(meth2:) withObject:theNumber waitUntilDone:NO];
Then your -meth2: method could look something like this:
- (void)meth2:(NSNumber*)theNumber
{
int next_int = [theNumber intValue];
// do whatever
}
It's a bit of a hack, but this works under ARC:
int next_int = 1;
[self performSelectorOnMainThread:#selector(meth2:)
withObject:(__bridge id)(void*)next_int
waitUntilDone:NO];
The __bridge keyword will tell the compiler to ignore reference counting under ARC, but it requires a pointer, so you first have to cast the int to a C style void pointer. When your method receives the message it will treat that object pointer as if it's an integer.
Note: If you can change the method to take an NSNumber instead of an integer, then that would be the "proper" fix. Unfortunately that's not always possible.
You can't use next_int as the withObject: because it's not an Object.
Change your call to:
[self performSelectorOnMainThread:#selector(meth2:)
withObject:[NSNumber numberWithInt:next_int] waitUntilDone:NO];
EDIT:
And change meth2 to expect an NSNumber instead of an int.