假设我在具有相同维度的树枝中有不同的简单数组:- (NSDictionary*) toDictionaryWithTraversalHistory:(NSMutableSet*)traversalHistory {
NSArray* attributes = [[[self entity] attributesByName] allKeys];
NSArray* relationships = [[[self entity] relationshipsByName] allKeys];
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity:
[attributes count] + [relationships count] + 1];
NSMutableSet *localTraversalHistory = nil;
if (traversalHistory == nil) {
localTraversalHistory = [NSMutableSet setWithCapacity:[attributes count] + [relationships count] + 1];
} else {
localTraversalHistory = traversalHistory;
}
[localTraversalHistory addObject:self];
//[dict setObject:[[self class] description] forKey:@"class"];
for (NSString* attr in attributes) {
if ([attr isEqualToString:@"description"]) {
continue;
}
NSObject* value = [self valueForKey:attr];
if (value != nil) {
if ([value isKindOfClass:[NSDate class]]) {
NSTimeInterval date = [(NSDate*)value timeIntervalSinceReferenceDate];
NSString *dateAttr = [NSString stringWithFormat:@"%@", attr];
[dict setObject:[NSNumber numberWithDouble:date] forKey:dateAttr];
} else {
[dict setObject:value forKey:attr];
}
}
}
for (NSString* relationship in relationships) {
NSObject* value = [self valueForKey:relationship];
if ([value isKindOfClass:[NSSet class]]) {
// To-many relationship
// The core data set holds a collection of managed objects
NSSet* relatedObjects = (NSSet*) value;
// Our set holds a collection of dictionaries
NSMutableArray* dictSet = [NSMutableArray arrayWithCapacity:[relatedObjects count]];
for (NSManagedObject* relatedObject in relatedObjects) {
if ([localTraversalHistory containsObject:relatedObject] == NO) {
[dictSet addObject:[relatedObject toDictionaryWithTraversalHistory:localTraversalHistory]];
}
}
[dict setObject:[NSArray arrayWithArray:dictSet] forKey:relationship];
}
else if ([value isKindOfClass:[NSOrderedSet class]]) {
// To-many relationship
// The core data set holds an ordered collection of managed objects
NSOrderedSet* relatedObjects = (NSOrderedSet*) value;
// Our ordered set holds a collection of dictionaries
NSMutableArray* dictSet = [NSMutableArray arrayWithCapacity:[relatedObjects count]];
for (NSManagedObject* relatedObject in relatedObjects) {
if ([localTraversalHistory containsObject:relatedObject] == NO) {
[dictSet addObject:[relatedObject toDictionaryWithTraversalHistory:localTraversalHistory]];
}
}
[dict setObject:[NSOrderedSet orderedSetWithArray:dictSet] forKey:relationship];
}
else if ([value isKindOfClass:[NSManagedObject class]]) {
// To-one relationship
NSManagedObject* relatedObject = (NSManagedObject*) value;
if ([localTraversalHistory containsObject:relatedObject] == NO) {
// Call toDictionary on the referenced object and put the result back into our dictionary.
[dict setObject:[relatedObject toDictionaryWithTraversalHistory:localTraversalHistory] forKey:relationship];
}
}
}
if (traversalHistory == nil) {
[localTraversalHistory removeAllObjects];
}
return dict;
}
- (NSDictionary*) toDictionary {
// Check to see there are any objects that should be skipped in the traversal.
// This method can be optionally implemented by NSManagedObject subclasses.
NSMutableSet *traversedObjects = nil;
if ([self respondsToSelector:@selector(serializationObjectsToSkip)]) {
traversedObjects = [self performSelector:@selector(serializationObjectsToSkip)];
}
return [self toDictionaryWithTraversalHistory:traversedObjects];
}
,arrayA
和arrayB
。像这样的语法不起作用:
arrayC
这两点都没有:
{% for A in arrayA, B in arrayB, C in arrayC %}
...
{% endfor %}
如何在Twig中的单个{% for A, B, C in arrayA, arrayB, arrayC %}
...
{% endfor %}
语句中访问多个数组?预先感谢。
答案 0 :(得分:2)
您不能在for循环中指定多个数组。你能做的最好就是
{% for i in 1..arrayA|length %}
{{ arrayA[i] }}{{ arrayB[i] }}{{ arrayC[i] }}
{% endfor %}
答案 1 :(得分:2)
如果您的密钥不是数字,您可以使用:
{% for key, A in arrayA %}
{{ arrayA[key] }} {{ arrayB[key] }} {{ arrayC[key] }}
{% endfor %}