Wednesday, November 30, 2011

Categories (By Scott Stevenson)

innen


Categories are one of the most useful features of Objective-C. Essentially, a category allows you to add methods to an existing class without subclassing it or needing to know any of the details of how it's implemented.

This is particularly useful because you can add methods to built-in objects. If you want to add a method to all instances of NSString in your application, you just add a category. There's no need to get everything to use a custom subclass.

For example, if I wanted to add a method to NSString to determine if the contents is a URL, it would look like this:
 
#import @interface NSString (Utilities) - (BOOL) isURL; @end
This is very similar to a class declaration. The differences are that there is no super class listed, and there's a name for the category in parenthesis. The name can be whatever you want, though it should communicate what the methods inside do.

Here's the implementation. Keep in mind this is not a good implementation of URL detection. We're just trying to get the concept of categories across:
 
#import "NSString-Utilities.h" @implementation NSString (Utilities) - (BOOL) isURL { if ( [self hasPrefix:@"http://"] ) return YES; else return NO; } @end
Now you can use this method on any NSString. The following code will print "string1 is a URL" in the console:
 
NSString* string1 = @"http://pixar.com/"; NSString* string2 = @"Pixar"; if ( [string1 isURL] ) NSLog (@"string1 is a URL"); if ( [string2 isURL] ) NSLog (@"string2 is a URL");
Unlike subclasses, categories can't add instance variables. You can, however, use categories to override existing methods in classes, but you should do so very carefully.

Remember, when you make changes to a class using a category, it affects all instances of that class throughout the application.

Thursday, November 3, 2011

NSTemporaryDirecotory

A lenyeg, hogy Simulator-ban nem az app sandbox-aba mutat mint pl az NSHomeDirectory()

Igy amig a device-on:
NSHomeDirectory() == /var/mobile/Applications/[APP_ID]
NSTemporaryDirectory() == /private/var/mobile/Applications/[APP_ID]/tmp/
addig iOS Simulatorban:
NSHomeDirectory() == /Users/[USER]/Library/Application Support/iPhone Simulator/5.0/Applications/[APP_ID]
NSTemporaryDirectory == /var/folders/oA/[ID]/-Tmp-/

Do you use the temporary directory? On Simulator NSTemporaryDirectory() returns Mac OS X tmp, a path in /var, which is outside the application sandbox.

#if TARGET_IPHONE_SIMULATOR
NSString *tmpPath = [NSHomeDirectory() stringByAppendingPathComponent: @"tmp"];
#else
NSString *tmpPath = NSTemporaryDirectory();
#endif

Tuesday, October 25, 2011

Halozat sebessegenek korlatozasa

Ha mondjuk 3G sebesseggek akarunk valamit simulatorban kiprobalni akkor jol johetnek ezek:

Ha Lion van a gepen, akkor Network Link Conditioner

Ha nincs Lion akkor is van par megoldas:

1. termialbol IP firewall rule-lal:

sudo ipfw add 500 pipe 1 ip from any to any 
sudo ipfw pipe 1 config bw 112kbit/s plr 0 delay 20ms
vissza pedig:
sudo ipfw delete 500

-50 USD, de van free trial
-Windows, Mac OS, Linux
-sok mas dologra is jo

-lehet domain-ekre korlatozni

Thursday, October 20, 2011

AgentM: A Better NSLog()

Itt van, meg 2005-bol.

Tuesday, October 18, 2011

Matt Gallagher: Variable argument lists in Cocoa

Itt van, de ami nekem kellett azt bemasolom:

The va_list, va_start, va_arg and va_end are all standard C syntax for handling variable arguments. To describe them simply:

  • va_list - A pointer to a list of variable arguments.
  • va_start - Initializes a va_list to point to the first argument after the argument specified.
  • va_arg - Fetches the next argument out of the list. You must specify the type of the argument (so that va_arg knows how many bytes to extract).
  • va_end - Releases any memory held by the va_list data structure.

Generally speaking, you can use this for loop for any variable argument situation where your arguments are all the same type. Other cases are a bit trickier but far less common — I'm sure you can work out how they would work if needed.

va_list in Cocoa

A number of classes in Cocoa have methods that take variable numbers of arguments. In most cases, these classes will also have an equivalent method that takes a va_list.

We can see an example of these va_list equivalents by looking at NSString. NSString declares the class method stringWithFormat:... (which takes a variable number of arguments) andNSString also declares the instance method initWithFormat:arguments: (where the argumentsparameter is a va_list) which handles the equivalent behavior of stringWithFormat:....

These va_list methods are used in the situation where your class defines a method with a variable argument list and you need to pass those variable arguments into the Cocoa method. For example, if the StringContainer class listed above declared the method:

- (void)setContentsWithFormat:(NSString *)formatString, ...;

The implementation of this method would be as follows:

- (void)setContentsWithFormat:(NSString *)formatString, ...
{
[contents autorelease];
va_list args;
va_start(args, formatString);
contents = [[NSString alloc] initWithFormat:formatString arguments:args];
va_end(args);
}

The va_list parameter allows us to pass our own variable argument list to the Cocoa method so that the Cocoa method can handle the arguments.