How do i check the key in dictionary is same as the string in method parameter?
i.e in below code , dictobj is NSMutableDictionary's object , and for each key in dictobj i need to compare with string. How to achieve this ? Should i typecase key to NSString??
-(void)CheckKeyWithString:(NSString *)string
{
//foreach key in NSMutableDictionary
for(id key in dictobj)
{
//Check if key is equal to string
if(key == string)// this is wrong since key is of type id and string is of NSString,Control doesn't come into this line
{
//do some operation
}
}
}
When you use the == operator, you are comparing pointer values. This will only work when the objects you are comparing are exactly the same object, at the same memory address. For example, this code will return These objects are different because although the strings are the same, they are stored at different locations in memory:
NSString* foo = #"Foo";
NSString* bar = [NSString stringWithFormat:#"%#",foo];
if(foo == bar)
NSLog(#"These objects are the same");
else
NSLog(#"These objects are different");
When you compare strings, you usually want to compare the textual content of the strings rather than their pointers, so you should the -isEqualToString: method of NSString. This code will return These strings are the same because it compares the value of the string objects rather than their pointer values:
NSString* foo = #"Foo";
NSString* bar = [NSString stringWithFormat:#"%#",foo];
if([foo isEqualToString:bar])
NSLog(#"These strings are the same");
else
NSLog(#"These string are different");
To compare arbitrary Objective-C objects you should use the more general isEqual: method of NSObject. -isEqualToString: is an optimized version of -isEqual: that you should use when you know both objects are NSString objects.
- (void)CheckKeyWithString:(NSString *)string
{
//foreach key in NSMutableDictionary
for(id key in dictobj)
{
//Check if key is equal to string
if([key isEqual:string])
{
//do some operation
}
}
}
Related
I've got an method that takes NSDictionary arg. This NSDictionary has some predefined keys it'll take. All the obj's should be strings. But only certain string objs are valid for each key.
So my approach was to typedef NSString for each valid string per key. I'm hoping not to extend the NSString class.
I've typedef'd some NSString's...
typedef NSString MyStringType
Then I define a few...
MyStringType * const ValidString = #"aValidString";
Here's what I'd like to do in my sample method..
- (void)setAttrbiutes:(NSDictionary *)attributes {
NSArray *keys = [attributes allKeys];
for (NSString *key in keys) {
if ([key isEqualToString:#"ValidKey"]) {
id obj = [attributes objectForKey:key];
//Here's where I'd like to check..
if (**obj is MyStringType**) {
}
}
}
}
I'm open to other ideas if there's a better approach to solve the obj type problem of an NSDictionary.
Doesn't work like that; typedefs are a compile time alias that don't survive being passed through a dictionary.
In any case, using typedefs for something like this would be unwieldy.
I suggest you create a property list -- either as a file in your project or in code -- that contains the specifications of your various keys and valid values, then write a little validator that, passed a string and value, can validate the string-value pair for validity.
This also gives you the flexibility to extend your validator in the future. For example, you might have a #"Duration" key that can only be in the range of 1 to 20.
Instead of setting up a typedef for you special values, one possible option would be to create an NSSet of the special values. Then in your code you can verify that the object in the dictionary is in your set.
What about a combination of category on NSString + associated object?
Something along the lines (untested!!):
#interface NSString (BBumSpecial)
- (NSString *) setSpecial: (BOOL) special ;
- (BOOL) special ;
#end
and:
#implementation NSString (BBumSpecial)
static void * key ;
- (NSString *) setSpecial: (BOOL) special {
objc_setAssociatedObject(self, &key, special ? #YES : #NO, OBJC_ASSOCIATION_ASSIGN) ;
return self ;
}
- (BOOL) special {
id obj = objc_getAssociatedObject(self, &key) ;
return obj && [obj boolValue] ;
}
#end
Which you could then use as:
NSString * mySpecialString = [#"I'm Special" setSpecial:YES] ;
?
I need to compare a key object from a div to a string. I access the key object in a for loop:
for (id key in _photosDic) {
...
}
In the loop, I want to compare the key with a string. Do I have to convert the key to a string?
id type can be anything so the first step would be to check if it is actually a NSString or not.
for that you can use this:
if ([idObject isKindOfClass:[NSString class]])
{
//Now do a simple casting
NSString *myString = (NSString *)idObject;
//Now compare the strings
NSComparisonResult result = [myOtherString compare:myString];
}
I have set up my simple Xcode project with a table that is binded to an array controller. It works fine if the array controller is full of entities with a string attribute. However I want to change the attribute to a BOOL and have the table show the string "true" or "false" based on the BOOL.
I have overrided the following two methods from NSFormatter:
-(NSString*) stringForObjectValue:(id)object {
//what is the object?
NSLog(#"object is: %#", object);
if(![object isKindOfClass: [ NSString class ] ] ) {
return nil;
}
//i'm tired....just output hello in the table!!
NSString *returnStr = [[NSString alloc] initWithFormat:#"hello"];
return returnStr;
}
-(BOOL)getObjectValue: (id*)object forString:string errorDescription:(NSString**)error {
if( object ) {
return YES;
}
return NO;
}
So the table gets populated with "hello" if the attribute is a string however if I switch it to a boolean, then the table gets populated with lots of blank spaces.
I don't know if this helps but on the line where I'm outputting the object, it outputs __NSCFString if the attribute is a string and "Text Cell" if I switch the attribute to a boolean. This is something else I don't understand.
Ok, it's not 100% clear what you're trying to do from the code, but first things first - BOOL is not an object, it's basically 0 or 1, so to place BOOL values into an array, you're probably best off using NSNumber:
NSNumber *boolValue = [NSNumber numberWithBool:YES];
and placing these into your array. Now you want to change your method:
-(NSString*) stringForObjectValue:(id)object {
NSNumber *number = (NSNumber *)object;
if ([number boolValue] == YES)
return #"true";
else
return #"false";
}
There's a few things here - for example, you want to avoid passing around id references if you can (if you know all your objects in the NSArray are NSNumber, you shouldn't need to).
How do you compare the text in two text fields to see if they are the same, such as in "Password" and "Confirm Password" text fields?
if (passwordField == passwordConfirmField) {
//they are equal to each other
} else {
//they are not equal to each other
}
In Objective-C you should use isEqualToString:, like so:
if ([passwordField.text isEqualToString:passwordConfirmField.text]) {
//they are equal to each other
} else {
//they are *not* equal to each other
}
NSString is a pointer type. When you use == you are actually comparing two memory addresses, not two values. The text properties of your fields are 2 different objects, with different addresses.
So == will always1 return false.
In Swift things are a bit different. The Swift String type conforms to the Equatable protocol. Meaning it provides you with equality by implementing the operator ==. Making the following code safe to use:
let string1: String = "text"
let string2: String = "text"
if string1 == string2 {
print("equal")
}
And what if string2 was declared as an NSString?
let string2: NSString = "text"
The use of == remains safe, thanks to some bridging done between String and NSString in Swift.
1: Funnily, if two NSString object have the same value, the compiler may do some optimization under the hood and re-use the same object. So it is possible that == could return true in some cases. Obviously this not something you want to rely upon.
You can do this by using the isEqualToString: method of NSString like so:
NSString *password = passwordField.text;
NSString *confirmPassword = passwordConfirmField.text;
if([password isEqualToString: confirmPassword]) {
// password correctly repeated
} else {
// nope, passwords don't match
}
Hope this helps!
I want to create a NSDictionary with +[NSDictionary dictionaryWithObjectsAndKeys:]. One of my keys has a string but the string can sometimes be nil. If the string is nil, any other value key pairs I put afterward will be ignored because the list is prematurely terminated. What is the standard way to deal with the possibility that there might be a value with nil in a NSDictionary?
You need to check if the string is null. If it is, add [NSNull null] instead of your string.
Creating NSDictionary objects can be combersome if you have many objects, that if they are nil, should not be included in the dictionary.
To me NSNull is as big a problem as it is the solution. When creating NSDictionary objects for usage in other objects, you can go two ways when dealing with nil values. Either you add NSNull objects to your dictionary - and then check for NSNull values when reading the values. This makes pretty code at the point of creation, but it becomes messy when reading out the values. Ex. should you check all keys in the dictionary, or are some garanteed to be not nil? And if an NSNull value is not filtered out it is bound to make exceptions when trying to send the object messages.
The second way is to just not add NSNull values to the dictionary. This is convinient when reading the NSDictionary, as the [someDictionary objectForKey:someKey] simply returns nil when the key is not set. This approach makes it pretty this easy when reading the values, but it really is bound to be messy code on creation. Ex. Creating a NSMutableDictionary, checking for nil before adding values, and finally returning an immutable copy?
Solution
To me the solution has been to create two categories, which in essense makes it easy for both the creating and the reading ends.
When creating the NSDictionary, you simply wrap you possible-nil values like this
[NSDictionary dictionaryWithObjectsAndKeysIngoringNull:
[NSNull nullWhenNil:val1], #"value1",
[NSNull nullWhenNil:val2], #"value2",
...
nil];
This code is almost as simple as approach number one, but it makes it substantially easier when reading out the values.
The categories is as follows (works in both ARC enabled and non-ARC code):
NSDictionary addition:
#implementation NSDictionary (NullAddition)
+ (id)dictionaryWithObjectsAndKeysIngnoringNull:(id)firstObject, ... {
NSMutableArray* objects = [NSMutableArray array];
NSMutableArray* keys = [NSMutableArray array];
va_list args;
va_start(args, firstObject);
for (id object = firstObject; object; object = va_arg(args, id)) {
id key = va_arg(args, id);
if (!key)
break;
if (![object isKindOfClass:[NSNull class]]) {
[objects addObject:object];
[keys addObject:key];
}
}
va_end(args);
return [self dictionaryWithObjects:objects forKeys:keys];
}
#end
NSNull addition
#implementation NSNull (NullAddition)
+ (id)nullWhenNil:(id)obj {
return (obj ? obj : [self null]);
}
#end
Good luck!
Attempting to insert data (key or value) into an NSDictionary will result in a Run Time Error. Therefore, you do not have to deal with null data in the dictionary.
'-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: Test)'
and
'-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
However, this means you are responsible for checking the validity of the data before putting it into the dictionary to prevent crashes.
I encounter a similar problem and google take me here. I need to add some kv into the dictionary whose value may be nil. Inspired by Trenskow's answer, I modify that function to support insert nil value, not terminate as before.
#define KV_END NSNull.null
NSDictionary *NSDictionaryOfKV(id firstKey, ...) {
__auto_type dict = [NSMutableDictionary dictionary];
va_list args;
va_start(args, firstKey);
for (id key = firstKey; key != NSNull.null; key = va_arg(args, id)) {
id obj = va_arg(args, id);
if (obj == NSNull.null) {
break;
}
if (key) {
dict[key] = obj;
}
}
va_end(args);
return dict.copy;
}
__auto_type value2 = nil;
__auto_type dict = NSDictionaryOfKV(key1, value1, key2, value2, key3, value3, KV_END);
You will got
{
key1: value1,
key3: value3
}
You can change the KV_END to anything you want.
This make sense when your value is a variable, you can use it as-is, don't need to wrap it or like value ?: NSNull.null.