2014-01-30 19 views
1

我是FRP和MVVM的新手,但到目前爲止它已經很好地發揮了作用,但現在觀察數組並將其摺疊爲字符串時出現問題。觀察數組並將其摺疊爲字符串

RAC(self, tags) = [[RACObserve(self.deal, tags) sequence] foldLeftWithStart:@"" reduce:^id(NSString *accumulator, NSString *value) { 
    return [NSString stringWithFormat:@"%@#%@ ", accumulator, value]; 
}]; 

上面的代碼只是暫停應用程序。

更新

所以我想通了,所以典型

RAC(self, tags) = [RACObserve(self.deal, tags) map:^id(NSArray *tags) { 
    return [[tags rac_sequence] foldLeftWithStart:@"" reduce:^id(NSString *accumulator, NSString *value) { 
     return [NSString stringWithFormat:@"%@#%@ ", accumulator, value]; 
    }]; 
}]; 

但是,這是最好的方法?

回答

2

如何:

RAC(self, tags) = [RACObserve(self.deal, tags) map:^(NSArray *tags) { 
    return [@"#" stringByAppendingString:[tags componentsJoinedByString:@"#"]]; 
}]; 
+0

並沒有完全得到所需的輸出,如果我會用'componentsJoinedByString:'它看起來像'[的NSString stringWithFormat:@ 「#%@」,[標籤componentsJoinedByString :@「#」]]'。 那麼哪一個是最具反應性的...... – mbogh

+2

啊,我的錯。我已經更新了我的答案。它不是「被動的」,但也不是使用序列,而是功能性的。 –

+0

類型切線相關,但也要在這裏看一下:https://github.com/ReactiveCocoa/ReactiveCocoa/issues/890 有一些關於序列,集合和信號的未來的討論,你可能會覺得有趣。 – drhr

相關問題