2011-03-25 65 views
11

對於我的應用程序,我試圖將CGRect對象存儲到NSMutableArray中。它正在加載並在日誌語句中打印,但試圖從數組中取出CGRect會顯示錯誤。這裏是一個代碼片段:從數組中獲取CGRect

CGRect lineRact = CGRectMake([[attributeDict objectForKey:@"x"] floatValue], 
    [[attributeDict objectForKey:@"y"] floatValue], 
    [[attributeDict objectForKey:@"width"] floatValue], 
    [[attributeDict objectForKey:@"height"] floatValue]); 

    [lineRactangle addObject:NSStringFromCGRect(lineRact)]; 

我怎樣才能從數組中得到rects?

回答

22
[lineRactangle addObject:[NSValue valueWithCGRect:lineRect]]; 
+0

感謝您的早期重播...... – ajay 2011-03-25 11:40:06

0

CGRect是一個結構,你不能把它放在NSArray中。你只能添加對象。

40

CGRect是一個結構體,而不是一個對象,因此不能存儲在NSArrays或NSDictionaries中。你可以把它變成一個字符串,把該字符串回一個的CGRect,但最好的方法是通過一個NSValue封裝它:

NSValue *myValue = [NSValue valueWithCGRect:myCGRect]; 

然後,您可以在存儲陣列和字典此NSValue對象。爲了把它放回一個的CGRect,你會怎麼做:

CGRect myOtherCGRect = [myValue CGRectValue]; 
+0

還是今天,'NSValue * myvalue的= [NSValue valueWithRect:myCGRect ];' – 2015-02-24 14:54:32

+1

@FitterMan:這隻適用於OS X,不適用於iOS; iOS上不提供'valueWithRect:'。但即使在OS X上,你也不應這樣做:'valueWithRect:'需要一個'NSRect',而不是'CGRect'。在64位構建中,'NSRect'是'CGRect'的一個類型定義,所以它在那裏工作沒有問題,但是對於32位構建,如果沒有定義'NS_BUILD_32_LIKE_64'定義,你會得到一個警告/錯誤因爲它們被定義爲兩個獨立的結構。儘管他們的內存表示是相同的,但編譯器(正確地)將它們視爲兩個不同的東西。 – DarkDust 2015-02-24 15:15:53

+0

感謝您的澄清。 – 2015-02-24 19:11:22

6

使用NSValueCGRect從而將它們存儲在NSArray秒。

例如:

CGRect r = CGRectMake(1,2,3,4); 
NSValue *v = [NSValue valueWithCGRect:rect]; 
NSArray *a = [NSArray arrayWithObject:v]; 
CGRect r2 = [[a lastObject] CGRectValue]; 

參見documentation爲其它支持的結構。

0

或更多的東西 '極端' ...創建保存的CGRect(S)

movPosTable = [[NSArray alloc] initWithObjects: 
      [[NSArray alloc] initWithObjects: [NSValue valueWithCGRect:[GridAB frame]], [NSValue valueWithCGRect:[GridBA frame]], [NSValue valueWithCGRect:[GridBB frame]], nil], 
      [[NSArray alloc] initWithObjects: [NSValue valueWithCGRect:[GridAA frame]], [NSValue valueWithCGRect:[GridAC frame]], [NSValue valueWithCGRect:[GridBA frame]], [NSValue valueWithCGRect:[GridBB frame]], [NSValue valueWithCGRect:[GridBC frame]], nil], 
      [[NSArray alloc] initWithObjects: [NSValue valueWithCGRect:[GridAB frame]], [NSValue valueWithCGRect:[GridBB frame]], [NSValue valueWithCGRect:[GridBC frame]], nil], nil]; 

的陣列的陣列,其中 'GridAA', 'GridAB' 等對應UIViews

+0

應該更好地添加此附錄作爲對所引用答案的評論,因爲我們無法將其視爲解決方案,並且它似乎是一條評論。 – iOS 2012-10-28 09:59:35

2

實際上,我認爲迄今爲止的答案都不能解決阿賈伊問的問題。簡短的回答是:您需要提供CGRectMakeintValue,而不是floatValue字典項目。如果你需要幾個CGRects做到這一點,這裏有一個建議的方法:

- (CGRect) NSArrayToCGRect: (NSDictionary *) attributeDict 
{ 
    int x = [[attributeDict objectForKey:@"x"] intValue]; 
    int y = [[attributeDict objectForKey:@"y"] intValue]; 
    int w = [[attributeDict objectForKey:@"width"] intValue]; 
    int h = [[attributeDict objectForKey:@"height"] intValue]; 

    return CGRectFromString([NSString stringWithFormat: @"{{%d,%d},{%d,%d}}", x, y, w, h]); 
} 

有可能做到這一點更優雅的方式,但上面的代碼不工作。

2

如果你的對象可以用 「.frame」 進行設置,你可以使用:

// {{CGFloat x,CGFloat y}, {CGFloat width,CGFloat height}} 
NSString *objectCoords = @"{{116,371},{85,42}}"; 
myObject.frame = CGRectFromString(objectcoords); 

或多個對象:

NSArray *objectCoords = [NSArray arrayWithObjects: 
         @"{{116,371},{85,42}}", 
         @"{{173,43},{85,42}}", 
         @"{{145,200},{85,42}}", 
         nil]; 

myObject1.frame = CGRectFromString([objectCoords objectAtIndex:0]); 
myObject2.frame = CGRectFromString([objectCoords objectAtIndex:1]); 
myObject3.frame = CGRectFromString([objectCoords objectAtIndex:2]);