即使存在将此问题标记为重复的风险,我也会问,因为我检查的所有相关问题都没有解决我的问题...
我有一个static func request(endpoint: String, query: [String: Any] = [:]) -> Observable<[String: Any]> {
do {
guard let url = URL(string: API)?.appendingPathComponent(endpoint) else {
throw EOError.invalidURL(endpoint)
}
return manager.rx.responseJSON(.get, url)
.map({ (response, json) -> [String: Any] in
guard let result = json as? [String: Any] else {
throw EOError.invalidJSON(url.absoluteString)
}
return result
})
} catch {
return Observable.empty()
}
}
static var posts: Observable<[Post]> = {
return NetworkRequest.request(endpoint: postEndpoint)
.map { data in
let posts = data["post"] as? [[String: Any]] ?? []
return posts
.flatMap(Post.init)
.sorted { $0.name < $1.name }
}
.shareReplay(1)
}()
class PostViewModel: NSObject {
let posts = Variable<[Post]>([])
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
posts
.asObservable()
.subscribe(onNext: { [weak self] _ in
DispatchQueue.main.async {
//self?.tableView?.reloadData() if you want to reload all tableview
self.tableView.insertRows(at: [IndexPath], with: UITableViewRowAnimation.none) // OR if you want to insert one or multiple rows.
//OR update UI
}
})
.disposed(by: disposeBag)
posts.asObservable()
.bind(to: tableView.rx.items) { [unowned self] (tableView: UITableView, index: Int, element: Posts) in
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell") as! PostCell
//for example you need to update the view model and cell with textfield.. if you want to update the ui with a cell then use cell.button.tap{}. hope it works for you.
cell.textField.rx.text
.orEmpty.asObservable()
.bind(to: self.posts.value[index].name!)
.disposed(by: cell.disposeBag)
return cell
}
startDownload()
}
}
func startDownload {
posts.value = NetworkRequest.posts
}
向量,我想找到与labs
变量中存储的3个组完全匹配的元素。
groups
我想确定&#34; BC-89HX&#34;组元素(不是&#34; BC-89HX含2%Puricare + 5%Merquat&#34;
set.seed(1)
labs <- sample(c(rep('BC-89HX',3), rep('BC-89HX with 2% Puricare + 5% Merquat',3), rep('Own SH',4)), 10)
labs
groups <- c('BC-89HX','BC-89HX with 2% Puricare + 5% Merquat','Own SH')
任何帮助?
答案 0 :(得分:2)
要确保“BC-89HX”是字符串中的唯一字符,并paste
^
$
和grep(paste0("^", groups[1], "$"), labs, value=TRUE)
#[1] "BC-89HX" "BC-89HX" "BC-89HX"
我们确定起始位置和结束位置的解决方案
fixed = TRUE
在这种情况下,我们无法使用^
作为$
,而fixed = TRUE
是元字符,暗示了起始位置和结束位置。如果我们执行==
,它会将其解析为“实验室”没有的文字字符
另一种选择是使用%in%
或labs[labs == groups[1]]
#[1] "BC-89HX" "BC-89HX" "BC-89HX"
labs[labs == groups[2]]
#[1] "BC-89HX with 2% Puricare + 5% Merquat" "BC-89HX with 2% Puricare + 5% Merquat" "BC-89HX with 2% Puricare + 5% Merquat"
,因为我们正在比较固定字符串而不是匹配字符串中的子字符串
grep
如果我们真的想将fixed = TRUE
与paste
一起使用,那么pattern
中的labs[grep(paste0("^", groups[2], "$"), paste0("^", labs, "$"), fixed = TRUE) ]
#[1] "BC-89HX with 2% Puricare + 5% Merquat" "BC-89HX with 2% Puricare + 5% Merquat" "BC-89HX with 2% Puricare + 5% Merquat"
labs[grep(paste0("^", groups[1], "$"), paste0("^", labs, "$"), fixed = TRUE) ]
#[1] "BC-89HX" "BC-89HX" "BC-89HX"
和具有相同字符的字符串就是template<typename T>
void foo(T &t)
{
for (auto iter = t.begin(); iter != t.end(); ++iter)
{
printf("%d\n", *iter); // will work for std::list<int> but not for std::map<int, int>
}
}
,即
onExecuteAction