Cocoa Tip: Filtering NSArray using NSPredicate

January 13th, 2010 vira Posted in Computer science, cocoa | No Comments »

Today I had a small task in one of my projects – to get subset of elements from NSArray. I’ve already started writing all this NSEnumerator-s stuff (need to support 10.4) when I remembered about NSPredicator. So, instead of iterating over an array and finding elements that satisfy some condition and adding them to some output array you just need to create predicate and filter your input array with it. Here is the sample code:

1
2
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"storeState == 1"];
NSArray  *outputArray = [inputArray filteredArrayUsingPredicate:predicate];

Predicates language seems to be very powerful and simple. But I can’t tell you anything about performance, need to investigate.

Further reading: Apple Predicates Programming Guide


Using Google AJAX search API in Cocoa

January 8th, 2010 vira Posted in Computer science, mac | 2 Comments »

Sometimes it’s needed to allow user to find some information in the Web, without interrupting he/she from working with your application. In this small article I’ll describe simple solution to fulfill this task.
As for today, search = Google and I’m going to implement search using it. I’ve implemented category for Cocoa’s NSArray class for creating array of dictionaries. Each dictionary represents a search results. You can define where to search (web, news, video) etc and how many results to return. This code requires JSON Framework by Stig Brautaset

Here is the code of the main search method. The full project can be downloaded here (XCode 3.2, JSON Framework included)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
+ (NSArray *)arrayWithSearchFor:(NSString *)query type:(NSString *)searchType range:(NSRange)range
{
    if (range.location > 56) {
    	NSLog(@"Error while searching in Google : start must be in range [0;56]");
    	return nil;
    }
 
    NSMutableArray *results = [NSMutableArray array];
    SBJSON *parser = nil;
    NSString *json_string = nil;
    NSUInteger i = range.location;
 
    while (i < range.length + range.location) {
        @try {
    	    NSURL *url = [NSURL URLWithString:[BASE_URL stringByAppendingFormat:@"%@?v=%@&q=%@&start=%i", searchType, @"1.0", query, i]];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                                        timeoutInterval:120];
            NSError *error = nil;
            NSURLResponse *response = nil;
            NSData *searchResults = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                        error:&error];
            if (!searchResults) {
                        @throw [NSException exceptionWithName:@"Failed to search cuz: %@" reason:[error description] userInfo:nil];
            }
            //parse results
            parser = [[SBJSON alloc] init];
            json_string = [[NSString alloc] initWithData:searchResults encoding:NSUTF8StringEncoding];
 
            //NSLog(@"Raw JSON result %@", json_string);
            NSDictionary *allResult = [parser objectWithString:json_string error:&error];
            if (!allResult || [allResult isKindOfClass:[NSNull class]]) {
                    @throw [NSException exceptionWithName:@"Failed to parse all results JSON" reason:[error description] userInfo:nil];
            }
            if (![[allResult objectForKey:@"responseStatus"] isEqualToNumber:[NSNumber numberWithInt:200]]) {
                @throw [NSException exceptionWithName:[allResult objectForKey:@"responseStatus"] reason:[allResult objectForKey:@"responseDetails"] userInfo:nil];
            }
            allResult = [allResult objectForKey:@"responseData"];
            if (!allResult || [allResult isKindOfClass:[NSNull class]]) {
                @throw [NSException exceptionWithName:@"Failed to parse responseData" reason:[error description] userInfo:nil];
            }
            NSArray *newResults = [allResult objectForKey:@"results"];
            if (!newResults) {
                @throw [NSException exceptionWithName:@"Failed to parse results" reason:[error description] userInfo:nil];
            }		
 
            [results addObjectsFromArray:newResults];
            i += SMALL_RESULT_SET;
        } @catch (NSException * e) {
            NSLog(@"Exception during searching in Google: %@", e);
        } @finally {
            [parser release];
            [json_string release];
        }
    }
    return results;
}
 
+ (NSArray *)arrayWithWebSearchFor:(NSString *)query range:(NSRange)range
{
    return [[self class] arrayWithSearchFor:query type:WEB_URL range:range];
}

And the sample usage:

1
2
3
4
5
NSArray *results = [NSArray arrayWithWebSearchFor:@"apple" range:NSMakeRange(0, 5)];
    NSLog(@"Search finished, found %i", [results count]);
    for (NSDictionary *result in results) {
        NSLog(@"Url = %@",  [result objectForKey:@"url"]);
    }

In this sample code I’ve searched for “apple” and got the following results:

2009-10-13 01:18:40.638 iGoogle[63665:a0f] Url = http://en.wikipedia.org/wiki/Apple_Inc.
2009-10-13 01:18:40.641 iGoogle[63665:a0f] Url = http://www.info.apple.com/
2009-10-13 01:18:40.641 iGoogle[63665:a0f] Url = http://www.crunchbase.com/company/apple
2009-10-13 01:18:40.642 iGoogle[63665:a0f] Url = http://developer.apple.com/
2009-10-13 01:18:40.642 iGoogle[63665:a0f] Url = http://apple.slashdot.org/
2009-10-13 01:18:40.642 iGoogle[63665:a0f] Url = http://finance.yahoo.com/q%3Fs%3DAapl
2009-10-13 01:18:40.644 iGoogle[63665:a0f] Url = http://support.apple.com/batteryprogram/
2009-10-13 01:18:40.644 iGoogle[63665:a0f] Url = http://topics.nytimes.com/topics/news/business/companies/apple_computer_inc/index.html

This code has never been used in production, so use on your own risk ;-)


Open all icons on your Mac

November 6th, 2009 vira Posted in fun, mac | No Comments »

Just for fun :) I decided to discover if it possible to open all icons (.icns files) with Preview and (as in real experiment) to measure how many resources this operation will take. To perform this I executed the following shell command:

find / -name *.icns -exec open {} \;

It is also better to change Preview preferences to make it open all files in one window (it isn’t a good idea to open thousands of windows). And here is the results for my system

MacBookPro5,4/Intel Core 2 Duo 2,53 GHz/4 GB DDR3/MacOS X 10.6.1

Preview resources consumption

So, it’s taken about 45 minutes and 850 MB of memory to found 4144 icons on my system… It’s possible to work with all this icons in Preview, search in them and so on. But generating of PDF file from all these icons failed (in fact I cancelled it when Preview had “ate” 2 GB of memory :) ). Here is screenshot of all this beauty.

All Icons in Preview

This could be useful for designers/programmers for researching/”stealing” icons.

E.g. I was surprised how the icon for “downloading in progress” file is drawn.

screen-shot-2009-11-06-at-00.22.17.png


Say no to Twitter

September 15th, 2009 vira Posted in internet | No Comments »

Твитер хорошо, но блог лучше :) Скоро начну опять писать что-то > 140 символов… надеюсь


People Net и Mac

April 17th, 2009 vira Posted in internet, mac | No Comments »

По причине отсутствия интернете в моем родном городе Комсмольске решилась я таки купить какое-нибудь 3G решение. Выбор пал на People Net, в большей степени из-за раскрученности и в меньшей после сравнения с конкурентами :)

Выбора модемов в отделении Приват-банка (где также можно подключиться) особого не было и “пришлось” брать Huawei EC 226. Не совсем пальцем в небо конечно – он поддерживает маки и вроде бы неплохой по отзывам.

На сайте people net-a в разделе этого модема есть драйвера под мак и инструкция, но! Сразу у меня ничего естественно не получилось. При подключении выдавало “No carrier detected…” Как оказалось, необходимо просто отключить проверку PIN-кода под Windows из программы Mobile Partner, которая поставляется с модемом. Жаль этого не описано в официальной инструкции.

И еще один момент, драйвера написаны довольно таки криво и забивают syslog всякой гадостью. Вроде бы связано с тем, что модем заявляет себя как cd-rom, но примаунтить его не выходит. Попытки постоянно повторяются и в результате получаем никому не нужную нагрузку на систему. Надо будет разбираться, более детально тут.

P.S. Скорость довольно таки нормальная, порой лучше киевской Воли кабель :)


XCode tips

March 31st, 2009 vira Posted in Computer science, mac | No Comments »

Here will be short list of the XCode hotkeys most used by me. I’ll try to update it periodically.

  • Switch to header/source file – Opt+Cmd+Up
  • Show Console – Shift+Cmd+R
  • Move to the next argument in Code Completion – Control+slash
  • Jump to definition – Cmd+doubleClick
  • Code Completion – Esc
  • Open Quickly – Shift+Cmd+D

Cool shortcut’s list for XCode


+1 мак девел – подарок Стиву Джобсу на 54-летие )

February 25th, 2009 vira Posted in trash | 2 Comments »

Чаще всего не верю в совпадения… но приходится порой :)  24-го февраля окончательно кинула все старое и перешла в мак девелоперы, а вечером узнала что в этот же день у Стива ДР… Хороший знак надеюсь :)


Eclipse tips: quick code assist

January 16th, 2009 vira Posted in Soft | No Comments »

Just a small tip I’ve discovered today:

Usually I use code assist in such way e.g.  to get System.out.print() press the following “S”+Ctrl+Space+Enter+dot+”o”+Enter+dot+”p”+Enter+”(”. But it can be done  faster – just enter “S”+Ctrl+Space+dot+”o”+dot+”p”+”(”. So Enter key can be omitted. Minus three keypressing -  cool :)

P.S. BTW, for such a commonl thing as System.out.println() code template “sysout” can be used.


Back your Mac to life

December 26th, 2008 vira Posted in mac | 1 Comment »

Перезапускаю свой ноут я обычно в случаях крайней необходимости – апдейты поставить или просто что-то перестало/стало медленно работать.

Так и случилось на днях, пришлось перезапустить после апйдетов Safari и iTunes… Но после перезапуска загрузка доходила только для логин скрина… В этом посте я попытаюсь вкратце передать свой опыт воскрешения макос-а.

Read the rest of this entry »


Magic of svn:keywords

December 11th, 2008 vira Posted in Computer science | 3 Comments »

Сегодняшний день не прошел без мини откровений  – узнала о возможности динамически изменять ревизию/дату изменения в исходниках при коммите. Для этого просто необходимо установить свойство svn:keywords (svn propset svn:keywords “Date Author” source.c) с необходимыми ключевыми словами (напр. Revision, Author и т.д.), а в исходнике добавить что-то вроде “$Revision$”. И при коммите вместо ключевого слова будет вставлен номер ревизии с которой он произведен -$Revision: 666$”

Одно из применений этого метода для Java – добавить в каждый класс статический блок

static{ System.out.println(”$Revision$ $Source$ $Date$”);}

При загрузке классов будет выводиться указанная информация, которая может быть крайне полезна при отладке – напр. один из классов устаревший.

Детали в svnbook