在使用一些遗留代码的项目中工作时,我发现了这个功能:
std::vector<std::string> Object::getTypes(){
static std::string types [] = {"type1","type2", "type3"};
return std::vector<std::string> (types , types +2);
}
Run Code Online (Sandbox Code Playgroud)
我可能会把它写成:
std::vector<std::string> Object::getTypes(){
std::vector<std::string> types;
types.push_back("type1");
types.push_back("type2");
types.push_back("type3");
return types;
}
Run Code Online (Sandbox Code Playgroud)
这仅仅是一种风格选择还是我缺少的东西?任何帮助将不胜感激.对不起,如果这太基础了.
更新: 实际上发现覆盖相同方法的不同类可以这样或那样做,所以它更加含糊不清.我会让它们都一样,但如果有的话,我会更喜欢更好的方法.
编辑
请注意,上面的遗留代码不正确,因为它只使用数组的前两个元素初始化向量.但是,此错误已在评论中讨论过,因此应予以保留.
正确的初始化应该如下所示:
...
return std::vector<std::string> (types, types + 3);
...
Run Code Online (Sandbox Code Playgroud) 我正在使用Parse跟踪我的项目的一些自定义事件.在我使用的第一个版本上:
[PFAnalytics trackEvent: @"Some event string"]; // This works
Run Code Online (Sandbox Code Playgroud)
但后来我决定跟踪更多事件,特别是应用内购买,所以我用这样的字典创建了更多的事件:
NSDictionary *dict = @{
@"Item name" : itemName,
@"Price" : priceString };
[PFAnalytics trackEvent:@"User Purchase" dimensions:dict];
Run Code Online (Sandbox Code Playgroud)
但不知何故,即使在发布第二个版本之后,此事件也没有显示在我的仪表板上,但@"Some event string"事件确实如此.有什么我做错了吗?
我已经检查了维度字典的键和值,但是一切看起来都很好,而且我甚至尝试过"User Purchase"没有维度的事件,但它也不起作用.
似乎这些代码非常简单,我在Parse里面的配置中有些东西.但后来我无法解释为什么第一个事件继续有效,PFAnalytics班级有没有错误?
更新: 似乎我不熟悉仪表板功能,我的事件正在被录制.您必须进入"自定义细分"才能看到自定义事件.现在我可以看到我的活动.
我有一个UItableViewCell名为的自定义类EditableTableViewCell(除了其他东西)有一个UITextField内部添加到其中contentView.在Textfield委托方法中,我试图检测它属于哪个部分.像这样:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
UIView* contentView =[textField superview];
//traverse up in the view hierarchy until we find the cell's content view.
while (![contentView isKindOfClass:[EditableTableViewCell class]] || contentView.superview == nil) {
contentView = [contentView superview];
}
EditableTableViewCell *thiscell = (EditableTableViewCell*)contentView;
NSIndexPath *indexpath =[[self tableView]indexPathForRowAtPoint:thiscell.center];
Run Code Online (Sandbox Code Playgroud)
这给了我正确的结果,我在这里看到了一个类似的解决方案 ,它也导航了视图层次结构.我想知道这是否是唯一的解决方案,或者是否有更合适的东西不涉及所有可用单元的循环.