简体   繁体   English

如何在同一个类的不同方法上重用从for循环派生的变量?

[英]How can I reuse a variable derived from a for-loop in a method on a different method in the same class?

Background: I'm working on automating a calendar app which contains a UICollectionView with many cells representing time slots. 背景:我正在使日历应用程序自动化,该应用程序包含一个UICollectionView,其中有许多表示时隙的单元格。
I have a CalendarView class with the Login class as its super class. 我有一个CalendarView类,其中Login类是其超类。 This CalendarView class contains all the methods I use in the calendar so that my tests look something like this CalendarView类包含我在日历中使用的所有方法,因此我的测试看起来像这样

func testMakeAppointment() {
    CalendarView()
        .moveToCorrectSchedule()
        .findEmptyCalendarCell()
        .tapNewAppointmentCell()
        .enterRegAppointmentInfo()
        .tapLatestAppointment()

On my findEmptyCalendarCell() I use a for-loop to locate the first available cell that's hittable on the calendar. 在我的findEmptyCalendarCell()我使用一个for循环来定位日历上可命中的第一个可用单元格。 The method looks like this: 该方法如下所示:

func findEmptyCalendarCell() ->CalendarView {
    let partialCellPath = XCUIApplication().collectionViews.childrenMatchingType(.Other)
    let start: UInt = 10
    let totalAmountOfCells = partialCellPath.count
    for i in start...totalAmountOfCells {
        if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].exists == true {
            if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].hittable == true {
                partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].tap()
            xctc.pauseForSeconds(1)
            }
        }
    }

    return CalendarView()
}

On the two following methods, I tap the cell I found and enter the appointment information and create the appointment. 在以下两种方法中,我点击找到的单元格,然后输入约会信息并创建约会。

Problem: Up to this point everything is good. 问题:至此一切都很好。 However, I now need to tap the appointment I just created in order to assert the appointment object now exists on the calendar. 但是,我现在需要点击我刚刚创建的约会,以便断言约会对象现在存在于日历中。 (This would be done under the tapLatestAppointment() method. (这可以在tapLatestAppointment()方法下完成。

If I could somehow reuse the i variable from the for-loop in findEmptyCalendarCell() to tap the same cell I used to create the appointment, that would be great. 如果我能以某种方式在findEmptyCalendarCell()的for循环中重用i变量来点击用于创建约会的同一单元格,那就太好了。 But unfortunately I can't seem to find a way to save this variable for later use. 但不幸的是,我似乎无法找到一种方法来保存此变量以备后用。

Could anyone please help me find a possible solution to this? 谁能帮我找到可能的解决方案吗? I tried making i a class variable of type UInt?, but since I return a new instance of CalendarView() after each method, this value is always nil :( 我尝试使i成为UInt类型的类变量,但是由于我在每个方法之后都返回一个CalendarView()的新实例,因此该值始终为nil :(

Thank you in advance 先感谢您

Certainly a far better model would be if a new CalendarView were not returned after each method call, but if you want to avoid rewriting your implementation you could just make i a global variable, declared outside any class. 当然,更好的模型是如果在每次方法调用之后都没有返回新的CalendarView ,但是如果您要避免重写实现,则可以使i成为全局变量,在任何类外部声明。 Alternatively you could create a class just for this purpose that would have a static property. 另外,您可以为此创建一个具有静态属性的类。

In a for loop, the value will only be available inside the loop. for循环中,该值仅在循环内部可用。 If this were not the case we would have thousands of variables floating around at every namespace for apps that used a lot of for s, memory would get hammered, and it would be a pain to code, quite frankly. 如果不是这种情况下,我们将有数以千计的变量漂浮在每个命名空间是使用了大量的应用for S,内存会得到敲定,这将是一个痛苦的代码,很坦率地说。

But based on your code what you really want is not a way to refer to i , which is changing on each for iteration, but a way to identify the last tapped index. 但是,根据您的代码,您真正想要的不是引用i ,后者在每次迭代时都for变化,而是一种识别最后一个分接索引的方法。

So how about something like: 那么怎么样:

func findEmptyCalendarCell() ->CalendarView {
    let partialCellPath = XCUIApplication().collectionViews.childrenMatchingType(.Other)
    let start: UInt = 10
    let totalAmountOfCells = partialCellPath.count
    for i in start...totalAmountOfCells {
        if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].exists == true {
            if partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].hittable == true {
                partialCellPath.elementBoundByIndex(i).otherElements["CalendarCell"].tap()
                selectedIndex = i  // See below
                xctc.pauseForSeconds(1)
            }
        }
    }

    return CalendarView()
}

Where selectedIndex would be defined at a global scope, at any file within your module and outside of any brackets, like so: 在全局范围内,在模块内以及任何方括号之外的任何文件中定义selectedIndex ,如下所示:

var selectedIndex: Int? = nil

Then under any situation where you want the selected index to go back to nil , just set it as such: 然后,在希望所选索引返回nil任何情况下,只需将其设置为:

selectedIndex = nil

In tapLatestAppointment , then, you would need to fall back when selectedIndex is nil . 然后,在tapLatestAppointment ,当selectedIndexnil时,您将需要后退。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将派生类方法委托给协议方法? - How do I delegate derived class method to protocol method? 如何在iPhone的相同方法上加载不同的视图 - How can i load a different view on same method in iphone 如何在同一个类中的awakeFromNib 方法中获取当前视图控制器? - How can I get the current viewcontroller in awakeFromNib method in the same class? 如何在同一个类中的另一个方法中使用CGPoint值 - how can I use a CGPoint value from one method in another within the same class 如何在实例方法中访问类方法变量,或者两者都使用公共变量? - How I can access Class method variable in instance method OR Use a common variable for both? 如何从UIViewcontroller类调用类别方法? - How can I call a category method from my UIViewcontroller class? 如何使用 objc_class 变量调用类方法 - how can I call class method by using objc_class variable 我可以使用相同的方法来传递不同的UIViewController子类吗? - Can I use the same method passing in different UIViewController subclasses? 如何在类方法中返回self? - How can I return self in a class method? 如何优化我的代码以使用一种方法,而不是在不同类中使用同一方法? - How can I optimize my code to use one method instead of multiple of same in different classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM