ios convert int to NSObject - ios

in my function I have a parameter of NSObject* type, to allow to pass NSString, NSDate, etc.. and intenally do something...
I need to pass also a value of type int..
how can I do this?
thanks

You can use the NSNumber class (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html), and initialize it with the numberWithInt method.

#Itamar's answer is sufficient, but I'm just simplifying it.
Try this,
NSNumber *your_object = [NSNumber numberWithInt:int_value];
So now int_value is converted into object.

Related

Extracting NSManagedObject attribute values from NSOrderedSet

I have an initializer that takes an array of Strings as a parameter. Rather than re-write the class and risk breaking things, I'd prefer to feed the initializer what it wants.
I'm trying to extract NSStrings from NSManagedObjects stored in an NSOrderedSet.
Here's what I've tried:
let soundsToPlay = sequenceToPlay.sounds as NSOrderedSet
for element in soundsToPlay {
// this prints out the object in console
print("\(element)")
// this, doesn't give me accessors for the sound object
// print("\(element.fileName)")
}
I'm missing something basic, but I'm not sure what. How would I enumerate the objects in an NSOrderedSet and extract the value of the attributes for the entities contained within the set?
I would suggest reading the documentation on KVC (Key Value Coding) as you can write this as one line of code:
let filenameArray = soundsToPlay.valueForKey("fileName").array()
The call to valueForKey will return an NSOrderedSet of the string and then you can convert that to an array with a call to array()
I figured it out. I was missing a step:
let soundsToPlay = sequenceToPlay.sounds as NSOrderedSet
for element in soundsToPlay {
// I have to tell the compiler what type of thing my thing is
let whatIWantToAccess = element as! MyObjectINeedAccessorsFor
print("\(whatIWantToAccess.fileName)")
}
Thats probably because compiler thinks that "element" is instance of NSManagedObject and it does not have fileName , try explicit type-casting , something like
for element: YourClass in soundsToPlay

How to box int (enum) to object in swift?

I need to cast an int to an object, in Objective-C I could do the following
[row.cellConfig setObject:#(UITextFieldViewModeAlways) forKey:#"textField.rightViewMode"];
What would be the Swift equivalent?
The Swift equivalent of UITextFieldViewModeAlways is
UITextFieldViewMode.Always, which is an enumeration value:
enum UITextFieldViewMode : Int {
case Never
case WhileEditing
case UnlessEditing
case Always
}
You get its underlying integer value with .rawValue.
Integers are automatically "bridged" to NSNumber when passed
to functions taking Objective-C parameters (and Swift strings
bridged to NSString).
So this should work:
row.cellConfig.setObject(UITextFieldViewMode.Always.rawValue,
forKey: "textField.rightViewMode")
For more information, see Using Swift with Cocoa and Objective-C.
Use
row.cellConfig.setObject(NSNumber(UITextFieldViewModeAlways), forKey:"textField.rightViewMode")
You need to create a NSNumber object for that int.
NSNumber *intObj = [NSNumber numberWithInt:num];

Difference between (BOOL) and boolValue in iOS

What is the difference between these two lines?
alertObj.AlertAddressed=[[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)] boolValue];
alertObj.AlertAddressed=(BOOL)[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)];
I'm getting a different result for these two lines - why?
Thanks in advance.
First one gives you actual bool value.
Second one type cast to BOOL the result of
[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)]
I don't know how good you are at pointers but I try to explain
First of all when you get an NSNumber it is an object, and the value of an object is at first it's pointer (so something like 0x0000af) this is simple an adress in the memory, and this address contains your NSNUmber wich contains the actual value (so let's say a bool information in your example)
It's pretty simple, when you do
(BOOL)alertObj.AlertAddressed=(BOOL)[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)];
what actually happen is the try to cast the 0x0000af part to a bool value... NOT what NSNumber contains (what you actually get depends on the current Pointer of the object when you try this)
alertObj.AlertAddressed=[[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)] boolValue];
this is something actually implemented in NSNumber and therefore it completly respects what the object does and gives you the bool saved in your NSNumber, and not a cast from it's pointer
NSNumber is an object, BOOL is a primitive type.
NSNumber is a class that wraps numbers, but you can't use to make operations, if you want you should unwrap calling a specific method. Since NSNumber is an object your variable is a pointer holding a reference to an object, not a value.
The first line is correct, the second is wrong because you are casting a pointer to a bool type.
alertObj.AlertAddressed=[[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)] boolValue]; This will convert you value to BOOL
alertObj.AlertAddressed=(BOOL)[NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)];This is like casting. Considers [NSNumber numberWithBool:sqlite3_column_int(compiledStatement, 9)] will return bool value.

Objective C : Array

I am new to objective C and trying to learn it. I am trying to write calculator program which performs simple mathematical calculation(addition, subtraction and so forth).
I want to create an array which stores for numbers(double value) and operands. Now, my pushOperand method takes ID as below:
-(void) pushOperand:(id)operand
{
[self.inputStack addObject:operand];
}
when I try to push double value as below:
- (IBAction)enterPressed:(UIButton *)sender
{
[self.brain pushOperand:[self.displayResult.text doubleValue]];
}
It gives my following error: "Sending 'double' to parameter of incompatible type 'id'"
I would appreciate if you guys can answer my following questions:
'id' is a generic type so I would assume it will work with any type without giving error above. Can you please help me understand the real reason behind the error?
How can I resolve this error?
id is a pointer to any class. Hence, it does not work with primitive types such as double or int which are neither pointers, nor objects. To store a primitive type in an NSArray, one must first wrap the primitive in an NSNumber object. This can be done in using alloc/init or with the new style object creation, as shown in the two snippets below.
old style
NSNumber *number = [[NSNumber alloc] initWithDouble:[self.displayResult.text doubleValue]];
[self.brain pushOperand:number];
new style
NSNumber *number = #( [self.displayResult.text doubleValue] );
[self.brain pushOperand:number];
I suggest using it with an NSNumber: Try not to abuse using id where you don't need to; lots of issues can arise if not.
- (void)pushOperand:(NSNumber *)operand
{
[self.inputStack addObject:operand];
}
- (IBAction)enterPressed:(UIButton *)sender
{
[self.brain pushOperand:#([self.displayResult.text doubleValue])];
}

ios NSUinteger to type 'id'

My brain is failing me today. I know this has got to be a simple one, but I just don't see it.
CGFloat *minutes = [self.displayData objectForKey:index];
Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'id'
index is a NSUInteger in a loop, (0 1 2 3 etc)
How do I get past this one? Thanks.
The dictionary waits for an object as the key (id) rather than a plain integer (NSUInteger).
Try wrapping the integer in a NSNumber object.
CGFloat *minutes = [self.displayData objectForKey:[NSNumber numberWithUnsignedInteger:index]];
The -objectForKey: method returns a value of type id, but you're trying to assign that to a CGFloat*, which isn't compatible with id because CGFloat isn't a class. If you know that the object you're retrieving from the dictionary is a certain type, say an NSNumber, you can take steps to convert it to a CGFloat when you get it.
Also, you're trying to use an int as a key into a dictionary, but dictionaries require their keys to be objects too. If you want to access your objects by index, store them in an array instead of a dictionary.
Putting it all together, you'd have something like this:
// after changing the displayData to an array
NSNumber *number = [self.displayData objectAtIndex:index];
CGFloat minutes = [number floatValue];
You should not be using a pointer like this:
CGFloat minutes = [self.displayData objectForKey:index];
Easy question. Just don't use NSUIntegrer. Instead of it, do the following:
id index;//then set the index

Resources