Archive for: 𔃲月 2014’

Python3 事始め

2014年2月21日 Posted by PURGE

■animal.py

class Animal:
    def __init__(self, name):
        self.name = name
        
    def call(self):
        print('こんにちは。%s さん' % (self.name))

■main.py

from animal import Animal
from person import Person

dog = Animal("犬")
cat = Animal("猫")
monkey = Animal("猿")

p1 = Person("太郎")

dog.call()
cat.call()
monkey.call()
p1.speak()
p1.call()

■出力

こんにちは。犬 さん
こんにちは。猫 さん
こんにちは。猿 さん
私は太郎 です。
こんにちは。太郎 さん

python3 での print format でのエラー

2014年2月21日 Posted by PURGE

正しい書き方かどうかまだ勉強中ですが、とりあえず、下記にて動作しました。

    def hello(self):
        print('こんにちは。%s さん' % (self.name))
        

NSRangeException

2014年2月15日 Posted by PURGE

どうやら配列絡みのエラーっぽい。

Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

下記のメソッドで返されたカウントより範囲を超えてしまっているようだ。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return items.count;
}

よくよく見ると、storybordでのtableViewのcontent設定が、Static Cells に設定されていたことに気づく。
ここは、Dynamic Prototypes 設定でしょ。

NSInternalInconsistencyException

2014年2月15日 Posted by PURGE

最近はプライベートで全くプログラムをやる気も無くなっていて久しぶりに iOSプログラミング。
そこで早速ハマったエラー。

テーブルビューが表示されないので、下記の戻り値を1に設定する。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

しかし、NSInternalInconsistencyExceptionエラーとなる。
どうやら、storyboardで画面遷移をしないので、何か矛盾が起きているようだ。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'unable to dequeue a cell with identifier Cell - 
must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

以下のコメントアウト部分でエラーとなっていた。
indexPathに見合うidentifierが見つからないようだ。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = @"セル";
    return cell;
}

結局、コメントアウト部分をコメントアウトして、cellのnil判定で初期化する処理を記述。
うまく行ったようです。

やっぱり、テンプレートで作成されたソースコードを良くみないと、自分で記述する場合と、storyboardを使用する場合の記述方法には、今後も気をつけないと。