2012-01-18 6 views
0

J'ai créé une méthode pour générer une chaîne attribuée à partir d'une chaîne normale. La valeur attrString ci-dessous est non nulle et correcte dans son contenu.iOS: CTFramesetterCreateWithAttributedString provoque EXC_BAD_ACCESS, pourquoi?

Lors de l'exécution de mon application, la deuxième ligne échoue avec un EXC_BAD_ACCESS. Une idée pourquoi?

L'appel __bridge_retained est une condition requise pour iOS 5. J'ai également essayé __bridge mais cela n'a aucun effet, le problème se produit toujours.

NSMutableAttributedString *attrString = (NSMutableAttributedString*)[textLayer attributedString]; 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge_retained CFMutableAttributedStringRef)attrString); 

MISE À JOUR

Si je fais:

NSMutableAttributedString *attrString = (NSMutableAttributedString*)[textLayer attributedString]; 
    NSLog(@"%@", attrString); 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFMutableAttributedStringRef)attrString); 

Ensuite, la ligne NSLog ne compilera pas; Xcode donne un avertissement et erreur indiquant "Incompatible pointer types passing 'char[3]' to parameter of type 'NSString *'".

donc j'ai essayé ceci:

NSLog([attrString description]); 

Et cela donne la sortie suivante (je l'ai remplacé le contenu de la chaîne avec <snipped> de la vie privée):

2012-01-19 06:49:11.307 WritePath[2475:707] <Snipped> { CTForegroundColor = "<CGColor 0x281530> [<CGColorSpace 0x22d1a0> (kCGColorSpaceDeviceRGB)] (0 0 0 1)"; NSFont = "<CGFont (0x28f850): ArialMT>"; NSParagraphStyle = "CTParagraphStyle:\nwriting direction = -1, alignment = 3, line break mode = 0, default tab interval = 0\nfirst line head indent = 0, head indent = 0, tail indent = 0\nline height multiple = 0, maximum line height = 0, minimum line height = 0\nline spacing adjustment = 0, paragraph spacing = 0, paragraph spacing before = 0\ntabs:\n<CFArray 0x287b50 [0x3f229630]>{type = immutable, count = 12, values = (\n\t0 : CTTextTab: location = 28, alignment = 0, options = (none)\n\n\t1 : CTTextTab: location = 56, alignment = 0, options = (none)\n\n\t2 : CTTextTab: location = 84, alignment = 0, options = (none)\n\n\t3 : CTTextTab: location = 112, alignment = 0, options = (none)\n\n\t4 : CTTextTab: location = 140, alignment = 0, options = (none)\n\n\t5 : CTTextTab: location = 168, alignment = 0, options = (none)\n\n\t6 : CTTextTab: location = 196, alignment = 0, options = (none)\n\n\t7 : CTTextTab: location = 224, alignment = 0, options = (none)\n\n\t8 : CTTextTab: location = 252, alignment = 0, options = (none)\n\n\t9 : CTTextTab: location = 280, alignment = 0, options = (none)\n\n\t10 : CTTextTab: location = 308, alignment = 0, options = (none)\n\n\t11 : CTTextTab: location = 336, alignment = 0, options = (none)\n\n)}"; }

+0

Vous devriez utiliser '__bridge' ici pour éviter une fuite de mémoire. Quelle est la sortie si vous 'NSLog ("% @ ", attrString)'? Collez la sortie dans votre question. –

+0

@rob Merci d'avoir répondu - J'ai fait ce que vous avez demandé, voir ci-dessus. – KomodoDave

+0

Désolé, j'aurais dû dire 'NSLog (@"% @ ", attrString)'. Mais ce que tu as fait est ok. –

Répondre

8

Je remarqué ceci dans votre décharge de chaîne attribuée:

NSFont = "<CGFont (0x28f850): ArialMT>" 

Avez-vous créé la chaîne attribuée en utilisant CGFontRef comme valeur pour la clé kCTFontAttributeName? Vous devez utiliser un CTFontRef, pas un CGFontRef. Ils sont différents.

+0

Je suis au travail maintenant, mais je vais vérifier quand je suis à la maison dans ~ 8 heures et répondre à nouveau. Merci beaucoup pour la suggestion et bien fait pour repérer ça, je parie que vous avez raison :-) Je suis conscient qu'ils sont différents mais j'ai une méthode automatique 'attributeString' dans une sous-classe CATextLayer que j'ai définie qui suppose un CTFont dans la propriété 'font' et je parie que j'ai par inadvertance mis un CGFont dans le constructeur (les deux sont acceptables pour cette propriété). – KomodoDave

+0

Tu avais raison Rob, la méthode fonctionne parfaitement maintenant :-) Merci beaucoup d'avoir pris le temps de répondre, ça m'aurait pris bien plus de temps à remarquer mon erreur. Pouces vers le haut! – KomodoDave

+0

Avait ce problème exact et j'ai trouvé que j'avais mis à la fois des objets UIFont et UIColor. Donc, merci, car cela a résolu le problème. – drekka

Questions connexes