OmegaDelta.net

Hong Kong

Objective-C: NSArray of weak references

2010-03-08 12:04

Need an array of weak references? e.g. for delegates. Here’s some good tips.

One convenient way is to box it in a NSValue with a non-retained object.

NSValue *value = [NSValue valueWithNonretainedObjectValue:myObj];
[array addObject:value];

and when you get the object:

value = [array objectAtIndex:x];
myObj = [value nonretainedObjectValue];

Simple Java ReplaceAll for string literals (i.e. no regex)

2010-03-03 19:41

Regex is great.  Really great.  But sometimes you just don’t want it.  Java provides a Stirng.replaceAll method but it uses regex and both parameters MUST be escaped if you just want a string literal replace.

Use Pattern.quote for the first param of Stirng.replaceAll, and Matcher.quoteReplacement for the second.

Here’s an example on how to escape the regex parameters:

String myString = “foo[]bar”;
myString = myString.replaceAll(Pattern.quote(“foo[]“), Matcher.quoteReplacement(“bar$”));
System.out.println(myString);
// output bar$bar

If you don’t believe me, try the above sample with

myString = myString.replaceAll(“foo[]“, “bar$”);

(you will get exceptions for both arguments if you try).

Hope that helped…

SVN – partial checkouts

2010-02-20 14:51

I have a project setup like this:

trunk/Project1
trunk/Project2

I wanted to add a common library to both projects.  Turns out there is a really nice way of doing this.

Add the library to trunk/Library

Then do a selective checkout.  Simply check out the trunk using --depth=empty (docs).  Then in this directory (which is a working copy with a .svn), you “svn update” the various directories you do want.  In this case, ’svn update Library’ and ’svn update Project1′.  You now have a perfectly valid, single working copy with just the libraries you need.

SVN also now has relative externs (since 1.5) so you could extern to ../Library which then still plays nice when you branch the trunk.  Only catch – it’s still an extern which requires separate committing and merging.  Less than ideal.  Still is it a hell of a lot better than absolute externs which require updating every time you branch as well.  I went with the partial checkout solution above, I think it’s neater, but it is nice to have the choice now.

[: too many arguments

2009-11-13 19:30

If you get the error “[: too many arguments" in your bash script - check that you have wrapped your variables with quotes.

if [ $X = "y"]

will cause this error if $X is a string with a space in it. Change to :

if [ "$X" = "y"]

Safer php filenames

2009-10-18 22:09

So you’re storing user files in your PHP app, but you are worried about security (as you should). Two bad things you might encounter (amongst others): a filename with “../” so as to overwrite arbitrary files on your server, or a filename with .php in it (to run arbitrary code).

I read this idea, but his method didn’t cut it for me.

Here’s a better one:

$str = preg_replace(array('/[^A-Za-z0-9_\-.]/', '/.php/'), '_', $str);

This nukes anything that’s not Alphanumeric or -,.,_. It also nukes any .php sequences.

For this I think a whitelist is better than a blacklist. Should I block anything else out?

Test case:

$str = '../tryToOverWiteMyScriptNow.php';
$str = preg_replace(array('/[^A-Za-z0-9_\-.]/', '/.php/'), '_', $str);
echo $str;

wow, OS X

2009-09-30 22:16

OS X really makes setting up a LAMP service dead easy. I need to do some offline dev (and lets face it, I’m very sick of uploading over slow connections).

Apache and PHP are installed out of the box. Yep – installed *by default* on everyone’s OS X install, not just developers. This is used for the web-sharing feature.

But you have to change one line in Apache to enable the PHP module. This doc was useful (I used vi for everything though, of course). Or the apple docs which make it look harder than it actually is.

Installing MySQL is as simple as downloading the DMG from MySQL.com and running the installer.

And that is is. No ./configure, no make, no make install. Just tweak some settings, and run an installer.

Love it!

svn ignores from the command line

2009-07-06 03:50

so I’m stuck in OS X without my beloved tortoise…

recursively set ignores by command line:

svn propset -R svn:ignore ‘*.blar’ .

SVN Move in Tortoise

2009-04-30 08:08

When you right click a versioned file it appears that there is no move function. Well there is! Read about it here.

I agree it’s non-obvious. I understand they reason why they had to do it the way they did though – my suggestion? Put a stub “Move” command in the normal context menu, with a dialog explaining where to actually find it. My first assumption was simply that Tortoise hadn’t implemented it…

svn ignore

2009-02-09 16:56

how to add svn ignores from the command line