CFURLCreateStringByAddingPercentEscapes returning NULL - iphone

I am having issues with a category method used to percent-escape illegal symbols.
This is the code that i am using for the task:
#implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
NSString *s = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",CFStringConvertNSStringEncodingToEncoding(encoding));
NSLog(#"S: %#, Self: %#", s, self);
return [s autorelease];
}
#end
When ever i run this method on a string without any of the symbols found in the above matching-string, the method runs fine and the same string is returned back to me.
For instance if i have a string like #"test" it will output:
S: test, Self: test
But if i instead use a string like #"test&symbols" it will output:
S: null, Self: test&symbols
Hence something seems to be wrong with the use of CFURLCreateStringByAddingPercentEscapes.
Now i want to escape symbols such as & because they can occur in strings used as values in a query string, which would cause the query string to be misinterpreted.
Any idea's about what may be the issue here?
Thank you in advance! / Magnus

After finding out my big mistake.
I was told to answer the question to my self.
What i did wrong that i didn't pass NSStringEncoding value to the method, like NSUTF8StringEncoding but instead of that I was passing a CF value such as kCFStringEncodingUTF8.
The value is passed thru a converter to make it CF value which caused an error and it was already had the correct type.
Sorry for any inconvenience.
-Magnus

Related

NSLog pointer syntax

I'm a little bit confused about the syntax of NSLog. For example,
NSString *nameString = #"Name";
NSLog(#"nameString is: %#", nameString);
If my understanding is correct (which it very well may not be), then nameString is defined to be a pointer to a String. I thought then that this would print the memory address that nameString holds, not the value of that address. So, if that is true, then in the NSLog statement, to get the value of the pointer, shouldn't we need to use the asterisk notation to access what nameString points to like this:
NSLog(#"nameString is: %#", *nameString);
?
It has been a little while since programming in C, but since Objective-C is a superset of C I thought they would behave similarly.
An explanation would be greatly appreciated! Thanks!
The command %# is like "shortcut" that calls the method -description on the receiver. For an NSString it simply display the string itself, since is inherited from NSObject you can override it, very usefull if you create for own class. In that case the default behaviur is print the value of the pointer. If you want to print the address of the pointer in the string just replace with :
NSLog(#"nameString is: %p", nameString)
I think that you use an asterisk only to declare a pointer. Then, you only use the name you decided. For example:
NSString *foo = [[NSString alloc] initWithString:#"Hello"];
NSLog(#"%#", foo);
Correct me if I am wrong :)
It's an object and NSLog is a function that uses its format specifiers to determine what to do with the argument. In this case the specifier is %# which tells NSLog to call a method on an object.
Normally this will call the method "description" which returns an NSString but it probably does respondsToMethod first and falls through to some other string methods.

is there any way to get the return value from a method by using #selector except using double pointer?

I don't want to use double pointer. I am using a function in simpler form as below.
-(NSString *) getName
{
return name;
}
So what is the correct way to take the returned NSString *?
By using #selector(getName) i am not able to get the returned value name.
Thank you in advance
You should use NSInvocation object instance for calling a selector and resolving returned result.
performSelector: does give you the return value directly.
NSString * s = #"NEXT WE HAVE NUMBER FOUR, 'CRUNCHY FROG'.";
NSString * l = [s performSelector:#selector(lowercaseString)];
NSLog(#"%#", l); // prints "next we have number four, 'crunchy frog'."

Write a replaceTokensWithStrings: method with varargs

I want to do the following:
["Hello <firstname> <middlename> <lastname>". replaceTokensWithStrings:
#"firstname", someFirstName,
#"middlename", middleNameMightBeNilObject,
#"lastname", lastNameObject];
It looks like this is not possible because of the "nil as a terminator problem".
However NSLog also takes multiple arguments and can handle nil as parameters too:
NSString *nilValue = nil;
NSLog(#"Value of nilValue=%#", nilValue);
output
Value of nilValue=(null)
So how can I achieve this in my replaceTokensWithStrings:... method?
Update: The signature of my method:
-(NSString *)replaceTokensWithStrings:(NSString *)input, ... NS_SOMETHING_SPECIAL_HERE{
NSLog counts the number of format specifiers (%#, %i, etc.) in its format string (the first argument, the literal string) in order to know how many arguments it should pull off the call stack. This is why it can handle nil arguments -- it is using the count to terminate its processing (and, incidentally, why it will often crash if you give it too few arguments).
Since you have a format-type string, you can do the same thing -- just whip up a helper method that counts the number of <something> elements in the string on which replaceTokensWithStrings: has been called, and use the results to limit your processing of the va_list.
The NS_SOMETHING_SPECIAL in your method declaration would be NS_REQUIRES_NIL_TERMINATION for the case where you're using nil as a sentinel. You don't need anything special if you're getting the count of arguments from somewhere.
Can't you change nil to be #"" so it will replace with a blank string?
Don't work?
["Hello <firstname> <middlename> <lastname>". replaceTokensWithStrings:
#"firstname", #"Peter",
#"middlename", #"", // or myStr
#"lastname", #"Smith"];
or check value:
#"middlename", myStr ? myStr : #"",
EDIT:
If you want variable parameters, you should read http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html

Return type from valueForKeyPath:?

This is probably pilot error on my part, but I am a little confused why this does not return an int (as thats the type of the property identified by the key path). Does valueForKeyPath: return an object instead, can anyone explain.
// Simple Object
#interface Hopper : NSObject
#property(nonatomic, assign) int mass;
#end
// Test
Hopper *hopper = [[Hopper alloc] init];
[hopper setMass:67];
NSLog(#"HOPPER: %d", [hopper valueForKeyPath:#"mass"]);
.
WARNING: Conversion specifies type 'int' but the argument has type 'id'
Yes, it returns an objc object:
- (id)valueForKeyPath:(NSString *)keyPath;
Details for automatic conversions from non-objc objects to objc objects (e.g. NSNumber and NSValue) is covered in Accessor Search Patterns for Simple Attributes.
Therefore, you would use the objc object format specifier %#:
NSLog(#"HOPPER: %#", [hopper valueForKeyPath:#"mass"]);
valueForKeyPath returns an object. int and char types are not objects. Access the property via the . operator or similar.
NSLog(#"HOPPER: %d", [hopper mass]);
NSLog(#"HOPPER: %d", hopper.mass);
Edit: Didn't fully read example code, updated answer

How can I see values of Object in NSLog?

Suppose I have an object containing some data.
How can I see that data using NSLog?
If anyone is not clear about my question, then can ask me again.
If you want to see an NSArray and NSDictionary and etc objects then you can directly print like NSLog(#"%#",object);
If it is an user defined object then you need to display by calling with property (attribute).
User defined object with name object and properties like
NSString *property1;
int property2;
NSMutableArray *property3;
Print them in the console as follows:
NSLog(#"%#, %d, %#" object.property1,object.property2,object.property3);
If you implement the -(NSString*)description method in your class then you can use NSLog to output a summary of the data. Of course, you can also directly output any property.
For example:
NSLog (#"%# %d", object, object.integer);
The first part calls the description method and outputs that; the second part gets the value of the integer property of object and outputs that.
Every Objective-c Object (this comes from NSObject) has a property called description. So if you want to print information about your class this is the way to go.
#implementation MyClass
- (NSString*)description
{
return [NSString stringWithFormat:#"MyClass:%#", #"This is my class"];
}
so if you do a call like this.
MyClass *myClass = [[MyClass alloc] init];
NSLog(#"%#", myClass);
NSLog(#"%#", [myClass description]); //Same as the line above
Then it will write "MyClass:This is my class" to the console (in this case it will print it twice).
Implement description of the given class.
-(NSString*)description {
return [NSString
stringWithFormat:#"<%#> name: `%#` size: `%#`",
NSStringFromClass(self), self.name,
NSStringFromCGSize(self.size)];
}
NSLog(#"%#", object); // <Object> name: `Harry` size: `{2, 2}`
extension Object: CustomStringConvertible {
var description: String {
"<\(Self.self)> name: `\(name)` size: `\(size)`"
}
}
print(object) // <Object> name: `Harry` size: `(2.0, 2.0)`
I would suggest these:
Objects:
For objects like Dictionary, Array, Strings do it like:
NSLog(#"%#", object);
For basic data-types like integers
NSLog(#"%i",intVal);
For type encoding you should see http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
Use this class https://github.com/arundevma/ICHObjectPrinter
NSLog(#"Object description is %#",[ICHObjectPrinter descriptionForObject:person]);
NSLog(#"My object data:%#",[myObj someData]);
NSLog(#"My object Other data:%#",[myObj someOtherData]);
Or directly:
NSLog(#"%#",myObj);
NSLog(#"Description:%#",[myObj description]);
Additionally to Satya's answer, if you want to see basic c data types, use the format specifiers. Such as %d for an integer:
NSLog (#"My integer:%d", myObject.myInteger);
The complete list is here:
http://www.cplusplus.com/reference/clibrary/cstdio/printf/