2016-07-07 51 views
2

我跟在標題下的Swift示例有沒有辦法從FAQ返回特定元素以嘗試從元素中檢索屬性。GREYActionBlock返回特定的元素屬性不起作用

我直接從FAQ中複製了示例,但在調用performAction之後,textValue仍然具有其原始值。事實上,無論我在動作塊中設置inout參數,變量都會在動作返回時保留其原始值。

我錯過了什麼?下面是我的代碼有:

func grey_getText(inout text: String) -> GREYActionBlock { 
    return GREYActionBlock.actionWithName("get text", 
     constraints: grey_respondsToSelector(Selector("text")), 
     performBlock: { element, errorOrNil -> Bool in 
      text = element.text 
      print("in block: \(text)") 
      return true 
    }) 
} 

,並在測試方法:

var textValue = "" 
let domainField = EarlGrey().selectElementWithMatcher(grey_text("Floor One")) 

domainField.assertWithMatcher(grey_sufficientlyVisible()) 
domainField.performAction(grey_getText(&textValue)) 

print("outside block: \(textValue)") 

打印

in block: Floor One 
outside block: 

我使用的XCode版本7.3.1

+0

這似乎與你使用塊的方式有關,如果你將'GREYActionBlock.actionWithName(...'直接賦值給var並使用它(而不是調用函數'grey_getText '得到它)? – Gautam

回答

3
func incrementer(inout x: Int) ->() ->() { 
    print("in incrementer \(x)") 
    func plusOne() { 
    print("in plusOne before \(x)") 
    x += 1; 
    print("in plusOne after \(x)") 
    } 
    return plusOne 
} 

var y = 0; 
let f = incrementer(&y) 
print("before \(y)") 
f(); 
print("after \(y)") 

雖然我們預計Y中1在執行結束時,Y保持爲0。下面是實際的輸出:

in incrementer 0 
before 0 
in plusOne before 0 
in plusOne after 1 
after 0 

這是因爲在輸出參數不是「call-by-reference」 ,但是「call-by-copy-restore」。正如由bootstraponline指出的PR所指定的那樣。