|
Convert a NSString to a C String |
|
|
Saturday, 12 November 2011 |
Next example show how to convert a C string to a NSString:
main.m // convert a NSString to a C string #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSString *str1 = @ "This is a NSString string";
NSData *str2 = [str1 dataUsingEncoding:NSASCIIStringEncoding ];
char const *str3 = str2. bytes;
printf ("Our C string is: %s", str3 );
}
return 0;
}
|
|
Last Updated ( Saturday, 12 November 2011 )
|
|
Convert a C String to a NSString String |
|
|
Saturday, 12 November 2011 |
Next short example shows you how to convert a C string into a NSString string:
main.m // convert a C string to NSString string #import <Foundation/Foundation.h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
const char *str1 = "This is a C string";
NSString *str2 = [NSString stringWithCString:str1 encoding:NSASCIIStringEncoding ];
NSLog (@ "Our NSString string is: '%@'", str2 );
}
return 0;
} Please note that NSStrings are UTF-8 encoded, C strings are usually ASCII.
|
|
Last Updated ( Saturday, 12 November 2011 )
|
|
Print a BOOL (boolean) value in Cocoa |
|
|
Sunday, 06 November 2011 |
To print a boolean value declared with BOOL we use:
main.m // print a BOOL (boolean) value #import <Foundation/Foundation.h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
BOOL var1 = YES;
BOOL var2 = NO;
NSLog (@ "var1 = %@",var1?@ "YES":@ "NO");
NSLog (@ "var2 = %@",var2?@ "YES":@ "NO");
}
return 0;
} |
|
Last Updated ( Sunday, 06 November 2011 )
|
|
Using selectors and introspection in Cocoa Touch |
|
|
Saturday, 05 November 2011 |
Using selectors and introspection in Cocoa Touch. See next example:
main.m // using selectors and introspection #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSMutableArray *array1 = [NSMutableArray array ];
NSString *element;
NSString *string1 = [NSString stringWithFormat:@ "This is a string"];
NSMutableString *string2 = [NSMutableString stringWithFormat:@ "This is a mutable string"];
NSMutableString *string3 = [NSMutableString stringWithFormat:@ "This is another mutable string"];
NSURL *url1 = [NSURL URLWithString: [NSString stringWithFormat:@ "http://www.freebsdonline.com"]];
NSProcessInfo *process1 = [NSProcessInfo processInfo ];
NSMutableDictionary *dict1 = [NSMutableDictionary dictionary ];
[dict1 setObject:@ "casa" forKey:@ "house"];
[dict1 setObject:@ "gato" forKey:@ "cat"];
[dict1 setObject:@ "perro" forKey:@ "dog"];
[dict1 setObject:@ "ratón" forKey:@ "mouse"];
[array1 addObject:string1 ];
[array1 addObject:string2 ];
[array1 addObject:string3 ];
[array1 addObject:url1 ];
[array1 addObject:process1 ];
[array1 addObject:dict1 ];
BOOL isMember;
BOOL isKind;
BOOL respondsToSelector;
for (element in array1 ) {
isMember = NO;
isKind = NO;
respondsToSelector = NO;
NSLog (@ "Name of the class: %@", [element className ]);
if ([element isMemberOfClass: [NSString class]]) {
isMember = YES;
}
NSLog (@ "Class is member of NSString: %@", isMember?@ "YES":@ "NO");
if ([element isKindOfClass: [NSString class]]) {
isKind = YES;
}
NSLog (@ "Class is kind of NSString: %@", isKind?@ "YES":@ "NO");
SEL selector1 = @selector (uppercaseString );
if ([element respondsToSelector:selector1 ]) {
respondsToSelector = YES;
}
NSLog (@ "Class responds touppercaseString: %@", respondsToSelector?@ "YES":@ "NO");
NSLog (@ "------------------------------------");
}
}
return 0;
}
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Iterate through a NSMutableArray |
|
|
Saturday, 05 November 2011 |
Let's say we want to display all elements of a NSMutable array. Here is the source code example for iterating through a NSMutable array:
main.m // iterate through a NSMutable array #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Display elements of an array");
NSString *element;
NSMutableArray *array;
array = [NSMutableArray arrayWithObjects:@ "value1", @ "value2", @ "value3", nil ];
for (element in array ) {
NSLog (@ "%@",element );
}
}
return 0;
}
|
|
Display Elements of a NSMutableDictionary object |
|
|
Saturday, 05 November 2011 |
This example shows you how to enumerate through values of a
NSMutableDictionary.
main.m // iterating through a NSMutableDictionary #import <Foundation/Foundation.h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Play With Strings");
NSMutableDictionary *dict = [NSMutableDictionary dictionary ];
// 'house' is the key, 'casa' is the data
// by the way 'casa' means 'house' in Spanish
[dict setObject:@ "casa" forKey:@ "house"];
[dict setObject:@ "gato" forKey:@ "cat"];
[dict setObject:@ "perro" forKey:@ "dog"];
[dict setObject:@ "ratón" forKey:@ "mouse"];
// Display an element of the dictionary using a key
NSString *word = [dict objectForKey:@ "dog"];
NSLog (@ "Spanish word for 'dog' is: %@", word );
// Example 1, Iterate through dictionary (fast enumeration
// by enumerating the key, then we use the key to get the value
for (id key in dict ) {
NSString *val2 = [dict objectForKey: key];
NSLog (@ "Key(english): %@, Value(spanish): %@", key, val2 );
}
// Example 2, Enumerate the values using NSEnumerator
NSEnumerator *enumerate = [dict objectEnumerator ];
id val;
while ((val = [enumerate nextObject ])) {
NSLog (@ "Value(spanish): %@",val );
}
}
return 0;
}
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Verify/check if a string begin with another string (check if a string has a suffix or prefix) |
|
|
Saturday, 05 November 2011 |
This example show you how to check if a string has a prefix or a suffix. For that we will use hasPrefix and hasSuffix methods:
main.m // check if a string has a sufix or a prefix #import <Foundation/Foundation.h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Verify if a string begins with a prefix");
NSString *str1 = @ "This is a string";
NSString *str2 = @ "Just another string";
if ([str1 hasPrefix:@ "This"])
NSLog (@ "str1 has 'This' prefix: '%@'",str1 );
if ([str2 hasSuffix:@ "string"])
NSLog (@ "str2 has 'string' suffix: '%@'",str2 );
}
return 0;
}
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Display Elements of a NSDictionary object |
|
|
Saturday, 05 November 2011 |
This example shows you how to enumerate through values of a NSDictionary. In next source code example there are two variants, first enumerates displaying values, second example will display keys and values.
main.m // example for displaying values from a NSDictionary object #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Play With Strings");
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @ "casa", @ "house",
@ "gato", @ "cat",
@ "perro", @ "dog",
@ "ratón", @ "mouse",
nil ];
// Display an element of the dictionary using a key
NSString *word = [dict objectForKey:@ "dog"];
NSLog (@ "Spanish word for 'dog' is: %@", word );
// Iterate through dictionary
NSEnumerator *enumerate = [dict objectEnumerator ];
id val;
int count= 1;
// First Variant, print values
while ((val = [enumerate nextObject ])) {
NSLog (@ "Word %d: %@",count, val );
count++;
}
// Second Variant, print values and keys
for (id key in dict ) {
NSString *val2 = [dict objectForKey:key ];
NSLog (@ "Key(english): %@, Value(spanish): %@", key, val2 );
}
}
return 0;
}
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Display elements of a NSArray array using fast enumeration |
|
|
Saturday, 05 November 2011 |
Let's say we want a display every element of a NSArray array. For that we will use fast enumeration:
for ( element in array ) { // code }
An example will make it easyer to understand:
main.m // display elements of a NSArray #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Display elements of an array");
NSString *element;
NSArray *array;
array = [NSArray arrayWithObjects:@ "value1", @ "value2", @ "value3", nil ];
for (element in array ) {
NSLog (@ "%@",element );
}
}
return 0;
}
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Transform a NSString into proper file system path using stringByStandardizingPath |
|
|
Saturday, 05 November 2011 |
Using stringByStandardizingPath we can transoform a NSString into a proper filesystem path.
main.m // transform a NSString into a proper filesystem path naming #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Path Examples");
NSString *path1 = @ "/usr/local/bin";
NSString *path2 = @ "~";
NSString *path3 = @ "/usr/local/../";
NSString *path4 = @ "/usr//local//bin/";
NSString *path5 = @ "/usr/./local/./bin/";
NSString *path_full;
path_full = [path1 stringByStandardizingPath ];
NSLog (@ "path1 is: %@", path_full );
path_full = [path2 stringByStandardizingPath ];
NSLog (@ "path2 is: %@", path_full );
path_full = [path3 stringByStandardizingPath ];
NSLog (@ "path3 is: %@", path_full );
path_full = [path4 stringByStandardizingPath ];
NSLog (@ "path4 is: %@", path_full );
path_full = [path5 stringByStandardizingPath ];
NSLog (@ "path5 is: %@", path_full );
}
return 0;
} |
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Display Full Homedir Path using stringByExpandingTildeInPath |
|
|
Saturday, 05 November 2011 |
Let's say we want to find full path for our homedir directory using "~" string which in OSX (also in Linux and UNICES in general) is short for homedir path. For that purpose we will transform "~" in full path using stringByExpandingTildeInPath method:
show_homedir_path.m #import <Foundation/Foundation.h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Display Homedir Path");
NSString *path_short = @ "~";
NSString *path_long;
path_long = [path_short stringByExpandingTildeInPath ];
NSLog (@ "Full Path is: %@", path_long );
}
return 0;
}For this purpose you can also use: stringByStandardizingPath.
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|
Define and Display a String in Objective C |
|
|
Saturday, 05 November 2011 |
For that we will use NSString Objects and NSLog:
Well in fact our example will display two strings str1 and str2:
DisplayStrings.m #import <Foundation/Foundation. h>
int main (int argc, const char * argv [])
{
@autoreleasepool {
NSLog (@ "Play With Strings");
NSString *str1 = @ "This is a string!";
NSString *str2 = @ "This is another string!";
NSLog (@ "Our strings are: %@ %@", str1, str2 );
}
return 0;
}
|
|
Last Updated ( Saturday, 05 November 2011 )
|
|