我想创建一个结构数组,其大小仅在运行时才知道
NSMutableArray *styleSettingsArray = [NSMutableArray array];
NSString *fontAlignmentAttribute = [element attributeNamed:@"TextAlignment"];
if(fontAlignmentAttribute)
{
CTTextAlignment alignment = [self getTextAlignment:fontAlignmentAttribute];
CTParagraphStyleSetting styleSetting = {kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &alignment};
[styleSettingsArray addObject:[NSValue valueWithBytes:&styleSettings objCType:@encode(CTParagraphStyleSetting)]];
}
// other posible attributes
CTParagraphStyleRef paragraphStyleRef = CTParagraphStyleCreate((__bridge const CTParagraphStyleSetting *)(styleSettingsArray), [styleSettingsArray count]);
[dictionary setObject:(__bridge id)(paragraphStyleRef) forKey:(NSString*)kCTParagraphStyleAttributeName];
CFRelease(paragraphStyleRef);
此代码不起作用。
编辑:
CTParagraphStyleCreate
接受一个指向 的数组的指针CTParagraphStyleSetting
,例如
CTParagraphStyleSetting styleSettings[] = {
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), alignment},
{...},
{...}
};
我如何分配这个数组,并在不知道里面有多少东西的情况下向它添加东西?(我如何使用 malloc ?)