Conversion from NSString to int not working [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have created an a nsstring idd variable in .h. and synthesized in .m. Now i have an int variable b and want to store the value of idd in b. Now when i convert idd to int. it not working the b always give me the 0 value.
.h
#property(retain, nonatomic) IBOutlet NSString *idd;
.m
int b=[idd intValue];
NSLog(#"the value of b=%d",b);

IBOutlets and IBActions are macros that mark variables and methods that can be referred to by Interface Builder to link UI elements to your code. They're typically linked to subclasses of NSResponder (like NSButton, NSView, etc.); not NSString's. Unless idd is bound to something in a NIB it won't have any value other than the default (zero). If idd is bound to a GUI object (control) then what you probably want is that controls value (in which case your code is correct).

idd probably doesn't have a value, or has a value that can't be parsed into an integer. Try NSLoging idd to see what it contains.

do
idd = #"2";
int b = [idd intValue];
NSLog(#"b = %i", b);
and that should display 2 as b

You need
int b=[self.idd intValue];

Related

How to associate a struct with object in Obj-C's runtime [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to use objc_setAssociatedObject to associate with the object?
OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
The value must be an Objective-C object. You need to wrap the struct in an Objective-C class. You could use NSValue for this.
StructType s = ...;
NSValue* value = [NSValue valueWithBytes:&s objCType:#encode(StructType)];
objc_setAssociatedObject(obj, SPECIAL_KEY, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
...
NSValue* value = objc_getAssociatedObject(obj, SPECIAL_KEY);
StructType s;
[value getValue:&s];

How dynamically initialise object in Swift 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I use below line of code to allocate an Object(Suppose my Object name is Car) dynamically.
[self initliazieObject:[Car class]]
- (id)initliazieObject:(Class)model{
id record = [[model alloc] init];
return record;
}
How I can do this in swift 3.
Exactly as in Objective-C. Try this in a playground:
class Car : NSObject {}
func factory(type:NSObject.Type) -> NSObject {
return type.init()
}
let c = factory(type:Car.self)
print(type(of:c)) // Car
(We can get fancy and do clever things with generics or Self to specify the type of the returned object more precisely, but my goal in this code is simply to do things the dumb way, just like Objective-C.)

testing object for key instead of keys value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm using valueForKey: to check isKindOfClass:. But some of these do not have a value and thus don't return anything. How do I test the actual key rather than the value of the key? If object.animal is an NSString with #"Cat" then obviously [[object valueForKey #"animal"] isKindOfClass:[NSString class]] checks out as a string. But if object.animal hasn't been given a value, I still want to know what kind of property animal was meant to be.
You can obtain this information using Objective-C runtime for properties. If you have
#property (readonly) NSString *animal;
You can use following code:
#import <objc/runtime.h>
// ...
objc_property_t property = class_getProperty(object.class, "animal");
const char* attr = (property != NULL) ? property_getAttributes(property) : NULL;
attr is C string, like following: "T#\"NSString\",R". You can extract class name from this string. See Apple documentation regarding property_getAttributes result. Note that this solution will only work, if animal is declared as property.
Much easier way to handle this situation:
#interface MyObject : NSObject
#property (readonly) NSString *animal;
+ (Class)animalClass;
#end
#implementation MyObject
+ (Class)animalClass {
return [NSString class];
}
#end
Answer to your question is here property type or class using reflection
Although I must point out using this is highly unadvisable. You might want to re think your approach if your really need to resort to this.

Mapping multiple Arrays in one Array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have two Arrays one array with list of items:
One array with string objects
and one with Array objects
NSArray * a=[1,2,nil];
NSArray * b=[[abc],[def],[ijk],[lmp], nil];
I want to return Array by mapping Array "a" object "1" to array "b" with objects "[abc],[def]"
and Array "a" object "2" to array "b" with objects "[ijk],[lmp]"
I know I can achieve it in NSDictionary but i want to return NSArray not NSDictionary.
or any alternative method to do it.
I think it's more about data structures knowledges?
I see that your pattern is to increase two index.
So, it's something you want?
NSMutableArray *object;
NSUInteger indexOfA = [a indexOfObject:#1]; // get the index of the object from A
for (NSUInteger index = 0; index < 2; index++) {
[object addObject:b[indexOfA * 2 + index]];
}
Then, you can pull out what you want from b.

what is the integer (number) equivalent to .text in objective c? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to put a number from a textfield on a Parse database and I need to specify that it is a number. How can I do this?
I have done the equivalent with a string using .text
Your question is unclear but I think you mean something like this:
UITextField *textField = ... // some text field
int age = [textField.text intValue];
user[#"age"] = #(age);
You need to convert the string value from the UITextField to a number, (e.g. int, double, or NSNumber).
NSString *textInput = myTextField.text;
int intValue = [textInput intValue];
NSNumber *age = [NSNumber numberWithInt:intValue];
user[#"age"] = age;

Resources