Codec ░ Objective-C

289Simulating Keyboard & Mouse Events. And Key-Modifier Events

Creating and Posting a Keyboard Event:


CGEventRef sDown, sUp;
sDown = CGEventCreateKeyboardEvent (
			NULL,
			(CGKeyCode)1,
			true
);
CGEventSetFlags(sDown, kCGEventFlagMaskShift);  

// setting flags with special function.
// Setting it via CGCreateKeyboardEvent
// would work only for the first time it's run

CGEventPost(kCGHIDEventTap, sDown);

sUp = CGEventCreateKeyboardEvent (
			NULL,
			(CGKeyCode)1,
			false
);
CGEventPost(kCGHIDEventTap, sUp);

CFRelease(sDown);
CFRelease(sUp);

That leaves the door open for applying the same to mouse events:

CGEventRef mouseEvent;
mouseEvent = CGEventCreateMouseEvent (
			NULL,
			kCGEventMouseMoved,
			CGPointMake(100, 100),
			kCGMouseButtonLeft
);
CGEventPost(kCGHIDEventTap, mouseEvent );

Magical. Isn’t it.

Of course, in pre-10.6 days it would have looked like that:

CGPostKeyboardEvent (0,5,true);
CGPostKeyboardEvent (0,5,false);

275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache 265Like explode(), only componentsSeparatedByString: 259The mystery of self.* – resolved? 218Obj-C, Shortcut: Boolean return value to String

275UIWebView – checking when user clicks a link

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

// intercepting web click in the webView
NSLog(@"%@, %i", [request.URL absoluteString], navigationType);
}

Don’t forget to set <UIWebViewDelegate>

289Simulating Keyboard & Mouse Events. And Key-Modifier Events 269Caching on Objective-C with NSURLCache 265Like explode(), only componentsSeparatedByString: 259The mystery of self.* – resolved? 220Linking libxml2 in Xcode

269Caching on Objective-C with NSURLCache

A quick reminder on how caching works in Objective-C.

Initially there is one cache, NSURLCache administrates it.

Get the object:

NSURLCache *cache = [NSURLCache sharedURLCache]

[cache memoryCapacity];
[cache setMemoryCapacity: 1024*1024*1]; // in byte
[cache currentMemoryUsage];

Don’t worry about the disk aka the flash memory in iPhone, it’s not accessible.

A bit of strange behaviour, either a bug in the software or in my brain:
When changing ViewControllers the memoryCapacity seems to reset itself to 0, although previous caches remain intact. Re-setting the memoryCapacity back to the original level seems to solve this. (The cache is not overwritten.)

Using the Cache
The whole point of using the cache is to not download online material over again. The following steps make for a happy cache:

//URLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:theURL
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];

The delegate response would allow you to alter the about-to-be-cached-data, leaving it in is not necessary:

- (NSCachedURLResponse *)connection:(NSURLConnection *)c
willCacheResponse:(NSCachedURLResponse *)response {
	NSLog(@"willCacheResponse");
	return response;
}

All in all, not that much to take it. Finding out, that the memoryCapacity gets reset was the most time-consuming bit.
For another example, of how to roll your own cache, look at URLCache example.

289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 265Like explode(), only componentsSeparatedByString: 259The mystery of self.* – resolved? 218Obj-C, Shortcut: Boolean return value to String

265Like explode(), only componentsSeparatedByString:

PHP
explode(“, ” , “One, Two, Three”);

Objective-C
NSArray *listItems = [@"One, Two, Three" componentsSeparatedByString:@", "];

319Chaning Colors in Twitter with the API 289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache 259The mystery of self.* – resolved?

259The mystery of self.* – resolved?

self
If you want to access a property of self using accessor methods, you must explicitly call out self as illustrated in this example:

self.age = 10; 

If you do not use self., you access the instance variable directly. In the following example, the set
accessor method for the age property is not invoked:

age = 10;

p57, The Objective-C 2.0 Programming Language Language (v2008-06-09)
p22, The Objective-C 2.0 Programming Language Language (v2009-10-19)

So basically it’s a shortcut to:

int* a = [self age];
[self setAge:12];

(providing there is a setter/getter for age.)

289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache 265Like explode(), only componentsSeparatedByString: 218Obj-C, Shortcut: Boolean return value to String

218Obj-C, Shortcut: Boolean return value to String

Turning Boolean 1, 0 into a more descriptive description:

NSLog(@”data1 is equal to data2: %@”, [data1 isEqualToData:data2] ? @”YES” : @”NO”);

376Variable length of accuracy of float in NSString 289Simulating Keyboard & Mouse Events. And Key-Modifier Events 287[^//]NSLog 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache

214NSMutableArray: setObject vs. setValue

Following situation Using TouchXML to parse XML data, works without problem on example XML files, but crahes on mine.
Problem Empty values in my data set. (sometimes <place>somePlace</place>, sometimes <place></place>)

/* CXMLDocument setup & parsing omitted */
NSString *e = [[resultElement childAtIndex:counter] stringValue];
NSString *k = [[resultElement childAtIndex:counter] name];

[blogItem setObject:e forKey:k];
// crashes when e is empty. Displays a (null), is nil

Solution 1
Check for empty e, replace nil with empty string

// check if element is empty
if ( nil == e ) {	// ...or the less elegant (0 == [e length])
	e = @"";
}
[blogItem setObject:e forKey:k];

Solution 2
Use setValue instead of setObject, as setObject crashes and burns when it encounters nil, whereas setValue specifically deals only with strings and handles nil gracefully.

[blogItem setValue:e forKey:k];

289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache 265Like explode(), only componentsSeparatedByString: 259The mystery of self.* – resolved?

212NSString and NSMutableString

Should have been clear, but was not:

NSString *s = @"aString";
s = @"anotherString";
NSLog(@"%@", s);
// anotherString

NSMutableString *m = @"aMutableString";
[m appendString:@"andAnAppendedString"];
NSLog(@"%@", m);
// aMutableStringandAnAppendedString

NString allows for the replacement of its whole content by simply assigning a new value.

With NSMutableString it is possible to add/delete/insert strings at arbitrary places in a string.

381Zero-Padding 376Variable length of accuracy of float in NSString 289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache

160@dynamic – Provide methods dynamically at runtime

“don’t worry about it, a method is on the way.”

http://theocacao.com/document.page/516

289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache 265Like explode(), only componentsSeparatedByString: 259The mystery of self.* – resolved?

112(NSError **)error

What is this ** doing again?

Oh yeah, thisand that.

289Simulating Keyboard & Mouse Events. And Key-Modifier Events 275UIWebView – checking when user clicks a link 269Caching on Objective-C with NSURLCache 265Like explode(), only componentsSeparatedByString: 259The mystery of self.* – resolved?