How to sort NSArray of custom objects by multiple properties – case insensitive

April 6th, 2012 No comments

Although sorting in XCode may look as something difficult, there are some quick solutions which allow us to sort array of object in less than 3 lines:

Let’s imagine that we have an array of objects. Each object has two properties: name and code. We want to sort our array by first criteria and then the other one.

We need to define sort descriptors which indicate object property to be sorted by and apply this sortDescriptors to a method sortedArrayUsingDescriptors of our NSMutableArray.

NSSortDescriptor *codeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
NSSortDescriptor *nameDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];

NSArray *sortDescriptors = [NSArray arrayWithObjects:codeDescriptor,nameDescriptor,nil];

shareClasses = [[NSArray alloc] initWithArray:[newShareClasses sortedArrayUsingDescriptors:sortDescriptors]];

 

Please note, that if you sort strings, it is sorted as strings. So if values are 1,2,3…10 …it is sorted:  1,10,2,3,4,5 ….

If you want to sort your array using integer values of strings you can write your own comparator (read about NSComparisonResult) or you can use “tricky” NSNumericSearch function inside your sortDescriptor:

NSSortDescriptor *codeDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES selector:@selector(caseInsensitiveCompare:) comparator:^(id obj1, id obj2) { return [obj1 compare:obj2 options:NSNumericSearch]; } ] autorelease];
This is the fastest method and you don't need to convert your string to integer for comparison.

 

Tags:

Jak wymusić start aplikacji w trybie landscape

January 9th, 2011 No comments

Aby wymusić tryb landscape musimy w kontrolerze, którego dotyczy operacja ustawić odpowiednią odpowiedź na metodę shouldAutorotateToInterfaceOrientation:

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

Jeżeli musimy zadbać o to, aby przy starcie aplikacji nasz ekran Default.png wyświetlał się w orientacji Landscape z uwzględnieniem obydwu wariantów – prawego i lewego. W tym celu w pliku Info.plist musimy dodać parametr:

<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeLeft</string>

A dodatkowo, dla zapewnienia poprawnej obsługi prawej i lewej strony:


<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>

W przypadku konieczności "przebudowania" widoku przy obrocie aplikacji musimy zadbać o poprawną obsługę metody:

(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self relayoutViews];
}

Tags:

How to use SVN in XCode

December 26th, 2010 No comments

It is quite common issue when we need to share our code with other developers or control versions of uploaded files. In corporate environment we usually have installed CVS or SVN. It may be also very helpful to use such environments in small companies or projects. One more advantage can also be, that we have backup on network drive in case of any issue with local storage.

How to install SVN quickly and start using it? The best approach would be to leverage systems delivered by our hosting provider. Dreamhost.com delivers ‘one-click install’ subversion. We just need to follow instruction displayed on the screen and after few minutes SVN is ready.
If we need just remote storage and files sharing – this is the only step we need to do. Now we need to install client on our machine OR we need to configure XCode.
For more advanced features we may configure additional access and scripts to deploy files on development, testing and production environments. Very good tutorial can be found on the page http://wiki.dreamhost.com/Subversion

There are few Mac clients for subversion. The most similar to Windows Tortoise SVN may be SCPlugin. Installation takes only few minutes and it gives all basic features. Installation is described on the page:

http://scplugin.tigris.org/

The best approach would be to configure repository directly in XCode. It gives direct access to all needed features and fully supports all features delivered in SCM – repositories management option of XCode. After installation we can use the “Commit” and “Update” commands (when you have files selected in the Groups & Files list) in the “SCM” menu in Xcode to download or upload changes to/from the repository.

To configure XCode:

  1. Open Xcode, and click “Preferences” under “Xcode”
  2. Select “SCM” option
  3. Click button “+” (add) under the “Repositories” to add new entry.
  4. Enter name, url, username and password. Xcode will also fill some wields automatically based on username, url and password.
  5. Choose your new repository from the list. Select it and click the “Checkout” button in the toolbar.
Tags:

Newsweek PL w wersji dla iPad – początek rewolucji

October 23rd, 2010 No comments

Pojawienie się aplikacji Newsweek PL i pierwszego darmowego numeru czasopisma w wersji na iPada zapowiada długo wyczekiwaną rewolucję w przejściu z papieru na wersje elektroniczne. A w zasadzie to już jest rewolucja – i to w doskonałym wykonaniu.
Październikowy numer Newsweek w wersji elektronicznej to już nie odbicie wersji papierowej, ale pełnoprawne wydanie przygotowane specjalnie dla iPada z wykorzystaniem wszystkich możliwości jakie dają tablety.

Pierwsze wrażenie po uruchomieniu aplikacji i załadowaniu pierwszego numeru jest rewelacyjne. Zdjęcie okładki w bardzo dobrej jakości, czytelna i zrozumiała nawigacja, poprawne wyświetlanie po obróceniu ekranu. Później jest już tylko lepiej – najlepiej pokazać to na obrazkach:

1. czytelna instrukcja obsługi i objaśnione dostępne funkcje:

2. możliwość powiększenia czcionki w artykule

3. W większości artykułów są interaktywne grafiki, a w artykule o Dronach można nawet obejrzeć model tego bezzałogowego samolotu w wersji 3d z możliwością samodzielnego obracania i powiększania.

W artykule o Hanibalu mamy do dyspozycji interaktywną mapę bitwy z możliwością wyświetlenia informacji o jednostkach, miejscach i zasobach

4. W elektronicznej wersji znajduje się kilkanaście filmów. Szczególnie ważne jest zastosowanie w miejscu gdzie czytamy recenzję filmu lub sztuki i możemy od razu kliknąć i obejrzeć jej fragment.

5. Reklama Mazdy zamieszczona w numerze to prawdopodobnie najbardziej zauważalna reklama w gazecie. Reklama wyświetla się jako oddzielny ekran z animacją lub interaktywną grafiką pomiędzy artykułami. Możemy ją oczywiście przewinąć, ale możliwość “tapnięcia” i obejrzenia jak wygląda auto po zmianie koloru lub jak jest wyposażone budzi większą ciekawość – warto pamiętać, że zdjęcia na iPadzie wyglądają rewelacyjnie.

Teoretycznie, sceptycy mogą stwierdzić, że większość tych funkcji można zrobić na stronie WWW.
Ale …. nikt jeszcze tego nie zrobił, a poza tym, nie zapominajmy, że wydanie Newsweeka ładujemy do iPada, zabieramy do samolotu i możemy czytać bez konieczności korzystania z sieci.
Czekamy na kolejne pisma, które podejmą próbę wyjścia z epoki papieru. Poprzeczka jest postawiona bardzo wysoko.

Tags:

How to create modal screen with save, email, share on facebook ?

October 16th, 2010 No comments


I want to add to my application new view with buttons for sharing content or saving it to disk. I know how to do sharing itself (save, email), but I wonder if this layer should be created as frame or there is any ready-to-use class displaying modal layer for half screen?

As I already know the answer, and it was much easier than I expected, I will share it here. Most programmers will probably find it in Human Interface Guidelines, but I started reading documentation from another documents, which was probably wrong order.
Read more…

Tags:

Pie Chart 3D Creator for iPhone

October 15th, 2010 1 comment

In my previous article I presented my new Pie Chart 3D library. I decided to use it in iPhone/iPad application. You can now enjoy this app and download it form AppStore.

Pie Chart 3D for iPad and iPhone
Pie Chart 3D Creator allows you to create pie chart wherever you are.
Just few taps and your pie chart is ready. You can save it for later or email immediately.

In version 1.0 you can:
- supports iPad and iPhone layout,
- supports rotation,
- add new slices (up to 12),
- move slices on a chart in slice settings,
- change slice size,
- rotate whole chart, just swipe your finger next to chart border,
- skew chart, just swipe up and down in the center,
- scale chart, use two fingers, like with pictures,
- save for later, in chart settings, chart is saved with date and chart name, (up to 15 saved charts),
- load saved charts, you will be warned that your current chart will be replaced,
- delete unneeded charts (just tap edit button on load list or swipe finger to right hand side),
- reset chart values to default 5 slices,
- email image with chart to any recipients email.

Chart uses two nice classes which I created and are available on my website:
- pie chart 3d class for drawing pie charts, with protocol returning clicked slice,
- color picker class allowing quick color selection from the list.

You can enjoy videos or purchase pure library.

IMPORTANT!!! IF YOU ADD YOUR COMMENT AND YOU REQUIRE ANSWER, AFTER 2-3 DAYS PLEASE CHECK YOUR SPAM FOLDER. A LOT OF EMAILS IS BLOCKED BY SPAM FILTER

Tags:

Color Picker library for iPhone

October 15th, 2010 No comments

When I created my Pie Chart Creator, I needed a class to display available colors and return clicked color after user selected color.
I created nice library – it may require some adjustments which I will add soon, but I want to share it now with others. Please remember, that it is based on new BSD license, which is explained in a source code and here

http://creativecommons.org/licenses/BSD/

Read more…

Tags:

Jak wysłać email z utworzonym obrazkiem lub widokiem?

October 4th, 2010 No comments

Bardzo często chcemy zachować efekt pracy naszego programu i umożliwić użytkownikowi wysłanie obrazu jako e-mail lub publikacje na portalach społecznościowych. Wbrew pozorom to znacznie łatwiejsza operacja niż się wydaje. Programiści Apple postarali się i można w łatwy sposób zapisać wybrany widok (UIView) jako obraz, a następnie dołączyć go do wiadomości e-mail.
Poniżej gotowy kod, który wystarczy podpiąć do własnego przycisku lub zdarzenia. Należy oczywiście wskazać odpowiedni widok, który chcemy zachować jako obraz.
Read more…

Tags:

Wykres Pie Chart na iPhone – Objective-C

September 29th, 2010 1 comment

[English description of Pie Chart Library is available at the bottom.I use this library in my Pie Chart 3D app - http://iphone.orpi.pl/?p=25 ]

Biblioteki graficzne iPhone nie zawierają zaawansowanych funkcji ani API do rysowania wykresów. Bardzo często w sieci pojawiają się pytania jak narysować wykres kołowy (pie chart).
Znalazłem kilka miejsc, gdzie opisane jest jak narysować płaski wykres wykorzystując linie i łuki. Nie jest to trudne – taką metodę można było już stosować w PHP. Dodatkowo Objective-C oferuje bogate możliwości wypełniania obszarów, pozostaje więc jedynie przypomnieć sobie kilka zasad trygonometrii (sinus i cosinus) i można rysować.

Nie znalazłem natomiast nigdzie rozwiązania do rysowania przestrzennych 3D. W PHP dodano bardzo fajny parametr style=IMG_ARC_PIE do rysowania łuku:
bool imagefilledarc ( resource $image , int $cx , int $cy , int $width , int $height , int $start , int $end , int $color , int $style )

parametr style=IMG_ARC_PIE rysuje fragment wykresu przestrzennego/ slice.

Niestety, w SDK 4.1 mamy możliwość rysowania tylko płaskich wykresów. A więc trzeba to oprogramować samodzielnie.

Postanowiłem przygotować własną bibliotekę, która będzie nie tylko rysowała wykresy płaskie, ale również wykresy 3D.

Poniżej film prezentujący możliwości biblioteki.
Wersja 1.0 wymaga jeszcze kilku poprawek. Planuję również dodanie możliwości wyświetlania etykiet.

Read more…

Tags:

Jak odczytać UUID urządzenia do instalacji w trybie AdHoc

September 17th, 2010 No comments

How to get iPad/iPhone UUID required for AdHoc installation?

1) Start iTunes and connect your iPad/iPhone. Click on device name on left navigation.
Uruchom iTUnes i podłącz iPad/iPhone. Kliknij na nazwę urzadzenia w lewym menu

2) On the Summary page for your iPhone, click on the word “Serial Number:” text
Na zakładce Podsumowanie, kliknij tekst “Numer Seryjny”

3) The serial number should change to the word “Identifier”, followed by a long string of numbers and digits
Numer seryjny zmieni się w Identyfikator (UUID) i 40 znakowy numer

4) When you press CTRL+C (Windows) or Command/AppleKey + C (Mac) and the UUID will be copied to the clipboard and can then be pasted into an YM/Email by Paste command or shortcut CTRL+V (Windows) or Command/AppleKey + V (Mac)

Po naciśnięciu CTRL+C (Windows) lub Command/AppleKey + C (Mac) numer zostanie skopiowany do schowka i może być wklejony na YM lub do wiadomości Email komendą WKLEJ lub klawiszami CTRL+V (Windows) lub Command/AppleKey + V (Mac)

Tags: