2

iOS 6.0 までの下位互換性を持つ iOS アプリケーションを作成しています。

iOS 7 では、NSStringインスタンス メソッドdrawInRect:withAttributes:が に置き換えられましdrawInRect:withFont:lineBreakMode:alignment:た。使用する方法を決定するために、次のコードがあります。

if ([NSString instancesRespondToSelector:@selector(drawInRect:withAttributes:)]) {
        NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
        [textStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [textStyle setAlignment:NSTextAlignmentLeft];
        [[self title] drawInRect:_textRect withAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:FONT_SIZE], NSParagraphStyleAttributeName: textStyle}];
}
else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [[self title] drawInRect:_textRect withFont:[UIFont boldSystemFontOfSize:FONT_SIZE] lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentLeft];
#pragma clang diagnostic pop
}

drawInRect:withAttributes:は iOS 7 で導入されたためinstancesRespondToSelector:、以前のバージョンの iOS で実行している場合は false を返す必要があります。ただし、iOS 6.1 を実行しているデバイスでこれをテストすると、true が返され、その後 を呼び出そうとするとクラッシュしますdrawInRect:withAttributes:。何が起こっているのか、または私が間違っていることを知っている人はいますか?

4

1 に答える 1

4

メソッドが特定のリリースの公開 API に含まれていなかったからといって、それが存在しなかったわけではありません。私の推測では、これ-drawInRect:withAttributes:は iOS 6 ではプライベート API でしたが、iOS 7 ではパブリックに昇格されました。ここで、iOS 6 でのみ true であることがわかっている他の条件をテストできます。例えばif (NSClassFromString(@"SomeiOS7OnlyClass") != Nil)

于 2013-11-05T21:34:46.143 に答える