AppleScript list/contains
AppleScript list/contains
set r to ""
set device to "IPHONE 6 PLUS SILVER 128GB-AUS"
set HighValueDevicesPass to {"IPHONE 7", "IPHONE 6", "IPAD PRO", "IPHONE 6S", "IPHONE 6 PLUS"}
if devices contains HighValueDevicesPass then
set r to "Pass"
end if
return r
我不明白为什么这不起作用。
变量是 "IPHONE 6 PLUS SILVER 128GB-AUS" 所以实际上包含列表中的 "IPHONE 6 PLUS"。
如果我使用 IF 在列表中,它工作正常,但我必须将所有不同的模型设置为变量。
如何进行部分匹配?
您可以检查字符串是否在列表中,但不能检查任意列表项是否在字符串中。您必须一项一项地重复所有项目。
set r to ""
set device to "IPHONE 6 PLUS SILVER 128GB-AUS"
set HighValueDevicesPass to {"IPHONE 7", "IPHONE 6", "IPAD PRO"}
repeat with i from 1 to count HighValueDevicesPass
if device contains item i of HighValueDevicesPass then
set r to "pass"
exit repeat
end if
end repeat
return r
我还删除了值 "IPHONE 6S" 和 "IPHONE 6 PLUS",因为它们已经与 "IPHONE 6" 字符串匹配。
set r to ""
set device to "IPHONE 6 PLUS SILVER 128GB-AUS"
set HighValueDevicesPass to {"IPHONE 7", "IPHONE 6", "IPAD PRO", "IPHONE 6S", "IPHONE 6 PLUS"}
if devices contains HighValueDevicesPass then
set r to "Pass"
end if
return r
我不明白为什么这不起作用。 变量是 "IPHONE 6 PLUS SILVER 128GB-AUS" 所以实际上包含列表中的 "IPHONE 6 PLUS"。
如果我使用 IF 在列表中,它工作正常,但我必须将所有不同的模型设置为变量。
如何进行部分匹配?
您可以检查字符串是否在列表中,但不能检查任意列表项是否在字符串中。您必须一项一项地重复所有项目。
set r to ""
set device to "IPHONE 6 PLUS SILVER 128GB-AUS"
set HighValueDevicesPass to {"IPHONE 7", "IPHONE 6", "IPAD PRO"}
repeat with i from 1 to count HighValueDevicesPass
if device contains item i of HighValueDevicesPass then
set r to "pass"
exit repeat
end if
end repeat
return r
我还删除了值 "IPHONE 6S" 和 "IPHONE 6 PLUS",因为它们已经与 "IPHONE 6" 字符串匹配。