Category Archives: Cocoa

Objective-C的???Geohash

以下はObjective-C的???Geohash | アクトインディ技術部隊報告書と同様のものです。

先日chibaさんがGeohashをdecodeするものをCLで書いていらしたので、それを微妙に参考にしながらCocoaのCoreLocationのCLLocationにgeohashをencode/decodeするメソッド追加してみました。

本当はビット演算がしたいのですが、文字列処理の方が自分には単純で簡単だったため、とりあえず今回は文字列処理にしてみました。

我ながらひどいコードだと思いますので、これからリファクタリングをしていきたいところです。

[CLLocation+Geohash.h]

#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>


@interface CLLocation(Geohash)

@property (nonatomic, readonly)   NSString    *geohash;

+ (CLLocation *)locationFromGeohash:(NSString *)aGeohash;

@end

[CLLocation+Geohash.m]

#import "CLLocation+Geohash.h"

#include <math.h>

NSString *int_to_binary(NSUInteger input)
{
    if (input == 1 || input == 0)
    {
        return [NSString stringWithFormat:@"%d", input];
    }

    return [NSString stringWithFormat:@"%@%d", int_to_binary(input / 2), input % 2];
}


double parse_binary(NSString *binary, double max, double min)
{
    double mid = 0.0;
    
    for (NSUInteger i = 0; i < [binary length]; ++i)
    {
        if ([binary characterAtIndex:i] == '1')
        {
            min = mid;
        }
        else
        {
            max = mid;
        }
        
        mid = (max + min) / 2;
    }
    
    return mid;
}


NSUInteger binary_to_int(NSString *input)
{
    NSUInteger result, length;

    result = 0;
    length = [input length];

    for (NSUInteger i = 0; i < length; ++i)
    {
        if ([input characterAtIndex:i] == '1')
        {
            result += pow(2, length - i - 1);
        }
    }

    return result;
}


NSString * generate_binary(double input, double max, double min, int cutoff)
{
    NSMutableString     *result;
    double              mid;

    result = [NSMutableString string];
    for (int i = 0; i < cutoff; ++i)
    {
        mid = (max + min) / 2;

        if (input > mid)
        {
            [result appendString:@"1"];
            min = mid;
        }
        else
        {
            [result appendString:@"0"];
            max = mid;
        }
    }

    return [NSString stringWithString:result];
}


@implementation CLLocation(Geohash)

- (NSString *)geohash
{
    int cutoff = 15;
    NSString *base32_characters  = @"0123456789bcdefghjkmnpqrstuvwxyz";

    NSString            *bin_lat, *bin_lng;
    NSMutableString     *bin_packed, *result;
    
    bin_lat = generate_binary([self coordinate].latitude, 90.0, -90.0, cutoff);
    bin_lng = generate_binary([self coordinate].longitude, 180.0, -180.0, cutoff);

    bin_packed = [NSMutableString string];

    for (int i = 0; i < [bin_lat length]; ++i)
    {
        [bin_packed appendFormat:@"%c%c", [bin_lng characterAtIndex:i], [bin_lat characterAtIndex:i]];
    }

    result = [NSMutableString string];

    // extract by 5-bit.
    for (int i = 0; i < [bin_packed length] / 5; ++i)
    {
        NSUInteger index;
        index = binary_to_int([bin_packed substringWithRange:NSMakeRange(i * 5, 5)]);
        [result appendFormat:@"%c", [base32_characters characterAtIndex:index]];
    }

    return result;
}


+ (CLLocation *)locationFromGeohash:(NSString *)aGeohash
{
    NSString *base32_characters  = @"0123456789bcdefghjkmnpqrstuvwxyz";

    NSMutableString *bin_packed, *bin_lat, *bin_lng;

    bin_packed = [NSMutableString string];

    for (NSUInteger i = 0; i < [aGeohash length]; ++i)
    {
        NSString *character;
        character = [[NSMutableString stringWithFormat:@"%c", [aGeohash characterAtIndex:i]] lowercaseString];

        for (NSUInteger j = 0; j < [base32_characters length]; ++j)
        {
            if ([character isEqualToString:[NSString stringWithFormat:@"%c", [base32_characters characterAtIndex:j]]])
            {
                NSMutableString *binary;
                binary = [NSMutableString stringWithFormat:@"%@", int_to_binary(j)];

                NSUInteger length = [binary length];
                for (NSUInteger k = 0; k < 5 - length; ++k)
                {
                    [binary insertString:@"0" atIndex:0];
                }

                [bin_packed appendString:binary];
                break;
            }
        }
    }

    bin_lat = [NSMutableString string];
    bin_lng = [NSMutableString string];

    for (NSUInteger i = 0; i < [bin_packed length]; ++i)
    {
        if (i % 2)
        {
            // a latitude is composed of odd bits. 
            [bin_lat appendFormat:@"%c", [bin_packed characterAtIndex:i]];
        }
        else
        {
            // a longitude is composed of even bits.
            [bin_lng appendFormat:@"%c", [bin_packed characterAtIndex:i]];
        }
    }

    return [[CLLocation alloc] initWithLatitude:parse_binary(bin_lat, 90.0, -90.0)
                                      longitude:parse_binary(bin_lng, 180.0, -180.0)];
}


@end

NSStringでかなり遅いcharacterAtIndexを使いまくっている。

参考

Geohash – Wikipedia, the free encyclopedia
GeoHashのdecodeのアルゴリズムの解説します & ScalaのGeoHashライブラリを作ってみました(仮) – ゆろよろ日記

Delaunay Tessellation for iPhone

以下の文章はDelaunay Tessellation for iPhone | アクトインディ技術部隊報告書と同様のものです。

プログラムプレゼンテーションの第2回は、iPhoneを用いてDelaunay分割をするというものにしてみました。

本当は他のものをやるつもりだったのですが、前回のプログラムプレゼンテーションではkomagataさんがObjective-Cのキメラさ、カオスさをおもしろがっていたと勝手に仮定して、Objective-C++でさらなるカオスさを目指すことにしました。

そんな折りに以前ドロネー分割をするためのプログラムをつくって、アップロードしようかなといっていたのを思い出しました。

ということで、今回はそれを流用することにしました。
単純に移植するだけだから簡単だろと思ったところ、意外と手直しに時間がかかってしまいました。

ドロネー分割については計算幾何学で出てくるものですが、この余白はそれを書くのにはせますぎるということで、ググればきっとわかる!!!

ソースコードはこちらです。
ライブラリとしてBoost C++ Librariesを使用しています。

実行すると、毎回ランダムな10点を作成し、下図のようにドロネー分割された図が表示されます。

Delaunay tessellation   Delaunay tessellation with circumcircles

右側の図は外接円も表示させた場合です。
ドロネー分割では生成された三角形によって作られる外接円の中に、その三角形以外の点が含まれてはいけません。

ランダムな点を生成する部分にはboost::randomを用い、ドロネー分割を計算する部分はC++で書かれています。

Objective-CとC++のコードを混ぜる場合の注意点は、HMDTさんによくまとめられているのでこちらを参照していただくのがよいかと思います。
HMDT – Objective-C++

Objective-C++のソースを書かれるときは、.mとなっている拡張子を.mmにしてくださいね。
自分はこれをしないでObjective-C++のファイルだと認識されずはまりまくりました。

こんな風にC++のコードも簡単に使用することができるので、Objective-Cを使われている皆様もぜひ組み合わせてみてはいかがでしょうか???

P.S.
本当はNSOperationを使って、スレッドを新しく作ってバックグラウンドで処理をしたかったのですが、プログラムの構造上面倒なのでやめました。

参考
Objective-C プログラミング言語:C++ と Objective-C の併用
HMDT – Objective-C++

複数行にわたったテキストを含むUILabelを適切な高さに調節する。

複数行にわたったテキストを表示するためのUILabelのインスタンスであるlabelがあり、そのlabelを適切なサイズ高さに変更したい時がありました。

// すでに以下の2行は他の部分で設定されている。
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:0];

CGRect *frame;

frame = [label frame];
frame.size = CGSizeMake(WIDTH, 0);

[label setFrame:frame];
[label setText:@"long long long text..."];
[label sizeToFit];

ポイントはnumberOfLinesを0にすることと、sizeToFitメッセージを呼び出すこと。
numberOfLinesを0にしないと、sizeToFitを呼び出した時点で、指定した幅が無視されてしまう気がする。

毎回frameの幅を決めているのは、テキストが何度も書き換わる場所だから。
テキストが短いものになった場合、sizeToFitメッセージで短い幅が設定されてしまうと以降ずっとその短い幅になったままになってしまうため。

でもAppleのドキュメントには、以下のようにsizeToFitメッセージはnumberOfLinesを考慮するって書いてある。
自分のやり方がおかしいのか?

UILabel Class Reference」より抜粋

When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

引用元: UILabel Class Reference

Apple Push Notification Serviceを利用した、iPhone クライアントと、Rubyによるサーバの作成。

[とりあえず表示しましたが、これからコードをアップロードしたり改変していきます。]

先週から、システム部で何か動くものを作って発表するという企画?が始まりました。
プログラムプレゼンテーションを始めてみたら意外に良かった

第一弾は自分だったので、1週間しかなかったので、ちょこっとやればできそうで今までやってみたかったiPhone OS 3.0の目玉機能の一つ、Push Notification Serviceの実装をやってみることにしてみました。

iPhoneクライアントはほぼObjective-Cしかありませんのでそれを使い、サーバはRubyで実装しました。

以下が発表したスライドを少し改変したものになります。

コードは今回はbitbucket.orgにアップロードすることにしました!
コードはこちらになります。
http://bitbucket.org/milkcocoa/apns-test/

ただしサーバのファイルに関しては

  • デバイスIDとかの部分はxでマスクしています。
  • キーに関してはスライドで指定した方法で記述してください。

[ポイントを書く。]

Xcodeで0xE8000001エラー(iPhone OS 2.2.1)

iPhone OS 2.2.1をインストールしたiPhoneにXcodeで作成したプログラムを転送しようとすると、

Your mobile device has encountered an unexpected error (0xE8000001)
Try disconnecting and powering off the device; then power the device on and reconnect it.

などというエラーが発生し、XcodeがiPhoneを認識しないという状況に陥ります。

これの解決策は

/var/mobile/Media/PublicStaging

を削除し(jailbreakする必要があります)、再起動することです。

なぜ発生するのかは、今の自分にはよくわかりません(jailbreakしたから???)。

UITableViewCellを削除ボタンを使って削除する

iPhoneのアプリケーションでは、交通標識の一時停止マークのようなものをクリックすると、削除ボタンが現れて項目が削除できるというギミックがあります。こういうのです。

このボタンを押して削除したときにはどのようなdelegateがおこなわれるか、そしてどのようにして項目を削除するのかということにちょびっとはまったので書いておきます。

まず、このボタンを押すとtableView:commitEditingStyle:forRowAtIndexPath:というdelegateがおこなわれます。

そして削除時なんですが、deleteRowsAtIndexPaths:withRowAnimation:メソッドを利用して削除します。
このとき注意すべき点は、このメソッドを呼び出した際にUITableViewのtableView:numberOfRowsInSection: delegateが呼び出されるということです。
そこで、UITableViewのcellに表示している項目を例えばNSMutableArrayなどで管理している場合には、そのNSMutableArrayの項目を先に削除しておかないと、例外が発生します。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [tableViewArray removeObjectAtIndex:row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

のように先にNSMutableArrayの方を削除してから、deleteRowsAtIndexPaths:withRowAnimation:メソッドを呼び出す訳です。