Showing posts with label initWithString. Show all posts
Showing posts with label initWithString. Show all posts

Saturday, May 18, 2013

NSString initWithString example ios


[NSString initWithString]

Returns an NSString object initialized by copying the characters from another given string.
- (id)initWithString:(NSString *)aString
Parameters
aString
The string from which to copy characters. This value must not be nil.

Important: Raises an NSInvalidArgumentException if aString is nil.
Return Value of [NSString initWithString]
An NSString object initialized by copying the characters from aString. The returned object may be different from the original receiver.
Example of [NSString initWithString]
NSString *remainingStr = nil;
if (remaining > 1)
    remainingStr = [[NSString alloc] initWithFormat:@"You have %d left to go!", remaining];
else if (remaining == 1)
    remainingStr = [[NSString alloc] initWithString:@"You have 1 left to go!"];
else
    remainingStr = [[NSString alloc] initWithString:@"You have them all!"];

NSString *msg = [NSString stringWithFormat:@"Level complete! %@", remainingStr];

[remainingStr release];

[self displayMessage:msg];
Example of [NSString initWithString]
NSString* myString = [[NSString alloc] initWithString:@"Test"];
[myString release];
myString = someOtherString;
Example of [NSString initWithString]
- (id)initWithString:(NSString *)str
{
    self = [super init];
    if (self != nil) {
        if ([str _isStringLiteral] || [str _isImmutable]) {
            [self release];
            return [str retain];
        }
        ...
}

Wednesday, May 1, 2013

NSURL initWithString example ios


initWithString:

Initializes an NSURL object with a provided string.
- (id)initWithString:(NSString *)URLString
Parameters of [NSURL initWithString]
URLString
The string with which to initialize the NSURL object. This string must conform to URL format as described in RFC 2396. This method parses URLString according to RFCs 1738 and 1808.
Return Value
An NSURL object initialized with URLString. If the string was malformed, returns nil.
Discussion of [NSURL initWithString]
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
Example of [NSURL initWithString]
NSString *webStr = [[NSString alloc] initWithFormat:@"%@",[webArray objectAtIndex:1]];

NSLog(@"urlString = %@",webStr); // its printing correct url string

NSURL *webURL = [[NSURL alloc] initWithString:[webStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

NSLog(@"url = %@",webURL); // it should print it

[webURL release];

[webStr release];
Example of [NSURL initWithString]
NSString *string    = [NSString stringWithFormat:@"http://abc.com  /Demo/View.php?drinkId=%@&name=%@&comment=%@&date=%@&rating=%@&    ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];

NSURL *url          = [[NSURL alloc] initWithString:string];