Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 快速操纵for循环内的对象_Swift - Fatal编程技术网

Swift 快速操纵for循环内的对象

Swift 快速操纵for循环内的对象,swift,Swift,我试图在for循环中为我的操作实例分配一个快照实例,但由于某种原因,它无法执行。当我在循环内设置断点时,我可以看到代码正在执行,但是在循环运行后检查操作对象后,它们都没有与之关联的快照s。我做错了什么 var i = 0 let shots = getShots() as! [Shot] for action in action_sequence{ if action.type == "shot" && i < Int(shots.count){ a

我试图在for循环中为我的
操作
实例分配一个
快照
实例,但由于某种原因,它无法执行。当我在循环内设置断点时,我可以看到代码正在执行,但是在循环运行后检查
操作
对象后,它们都没有与之关联的
快照
s。我做错了什么

var i = 0
let shots = getShots() as! [Shot]
for action in action_sequence{
    if action.type == "shot" && i < Int(shots.count){
        action.shot = shots[i] //->Breakpoint set. This code is being executed.
        i += 1
    }
}
//->Breakpoint set and none of the action instances have shots defined.
编辑 在发布代码之前,我经常尝试简化代码,以便让其他人更容易理解。但显然,简化我的代码以某种方式解决了这个问题。下面是实际情况:

@interface Session : NSObject
@property float average_score;
...
-(int)shotCount;
-(NSArray*)getShots;
@end


@interface Shot : NSObject
@property float score, x, y;
@property long long time_stamp;
...


class Drill: NSObject {
    ...
    var action_sequence = [DrillAction]() 


class DrillReviewController: UIViewController, UITableViewDelegate, UITableViewDataSourc{
    var drill : Drill?
    var session: Session()
    ...

    override func refreshView(){
        super.refreshView()
        //load the drill sequence with the shots from the session.

        var i = 0
        let shots = self.session.getShots() as! [Shot]
        for action in self.drill!.action_sequence{
            if action.type == "shot" && i < Int(self.session.shotCount()){
                action.shot = shots[i]
                i += 1
            }
        }    
        self.tableView.reloadData()
    }
}
@接口会话:NSObject
@房地产浮动平均分;
...
-(int)散弹次数;
-(NSArray*)获取快照;
@结束
@接口快照:NSObject
@财产浮动分数,x,y;
@物业长时间加盖公章;
...
类钻取:NSObject{
...
var action_sequence=[DrillAction]()
类DrillReviewController:UIViewController、UITableViewDelegate、UITableViewDataSourc{
钻孔机:钻孔机?
var会话:会话()
...
重写函数刷新视图(){
super.refreshView()
//使用会话中的快照加载演练序列。
变量i=0
让shots=self.session.getShots()作为![Shot]
对于self.drill!.action\u序列中的操作{
如果action.type==“shot”&&i
在您的问题中,您没有显示问题的原因。它位于其他地方。可能是您的测试方式,也可能是有关
快照的内容

在这样的情况下,通过将问题简化为更简单的问题来进行测试,例如(您没有选择揭示快照是什么,所以我只是编了一些东西):

@objc类操作:NSObject{
射击:射击?
var name=“”
var complete=false
var type=“shot”
}
@objc类快照:NSObject{}
让动作_序列=[action()]
变量i=0
让快照=[Shot()]
对于动作序列中的动作{
如果action.type==“shot”&&i

这是有效的,因此您的基本代码必须有效。(您的代码也相当愚蠢,因为您可以编写得更紧凑,而不需要使用计数变量
i
。但这不是您所要求的。)

在您的问题中,您没有显示问题的原因。它位于其他地方。可能是您的测试方式,也可能是有关
快照的内容

在这样的情况下,通过将问题简化为更简单的问题来进行测试,例如(您没有选择揭示快照是什么,所以我只是编了一些东西):

@objc类操作:NSObject{
射击:射击?
var name=“”
var complete=false
var type=“shot”
}
@objc类快照:NSObject{}
让动作_序列=[action()]
变量i=0
让快照=[Shot()]
对于动作序列中的动作{
如果action.type==“shot”&&i

这是有效的,所以您的基本代码必须有效。(您的代码也相当愚蠢,因为您可以编写得更紧凑,而不使用计数变量
i
。但这不是您要问的。)

如果它是一个模型对象(Action),为什么不使用struct呢?与问题无关,但为什么要从
Int
.count
)创建
Int
?@vadian整个代码都很愚蠢如果在for循环之前键入
print(shots)
,会发生什么?@matt我们在这里要有建设性。@vadian谢谢。我以前有
session.getShotCount()
返回Int32,因此我需要将其更改为Int。@Umar我仍在尝试适应swift。我将研究如何使用结构。@PranavKasetti我看到了我预期的所有快照。如果结构是模型对象(操作),为什么不改为使用结构?与问题无关,但为什么要从
Int
.count
)创建
Int
?@vadian整个代码都很愚蠢如果在for循环之前键入
print(shots)
,会发生什么?@matt我们在这里要有建设性。@vadian谢谢。我以前有
session.getShotCount()
返回Int32,因此我需要将其更改为Int。@Umar我仍在尝试适应swift。我将研究如何使用struct执行此操作。@PranavKasetti我看到了我预期的所有快照。
@interface Session : NSObject
@property float average_score;
...
-(int)shotCount;
-(NSArray*)getShots;
@end


@interface Shot : NSObject
@property float score, x, y;
@property long long time_stamp;
...


class Drill: NSObject {
    ...
    var action_sequence = [DrillAction]() 


class DrillReviewController: UIViewController, UITableViewDelegate, UITableViewDataSourc{
    var drill : Drill?
    var session: Session()
    ...

    override func refreshView(){
        super.refreshView()
        //load the drill sequence with the shots from the session.

        var i = 0
        let shots = self.session.getShots() as! [Shot]
        for action in self.drill!.action_sequence{
            if action.type == "shot" && i < Int(self.session.shotCount()){
                action.shot = shots[i]
                i += 1
            }
        }    
        self.tableView.reloadData()
    }
}
@objc class Action: NSObject {
    var shot:Shot?
    var name = ""
    var complete = false
    var type = "shot"
}
@objc class Shot : NSObject {}

let action_sequence = [Action()]
var i = 0
let shots = [Shot()]
for action in action_sequence{
    if action.type == "shot" && i < Int(shots.count){
        action.shot = shots[i]
        i += 1
    }
}
print(action_sequence[0].shot) // it is a Shot, not nil