Använda Upprepa-åtgärder i Genvägar
Åtgärderna Upprepa och åtgärden Upprepa med varje för det möjligt att köra en grupp av åtgärder i en genväg flera gånger i rad.
Använda Upprepa-åtgärden
När du lägger till en Upprepa-åtgärd till din genväg innehåller åtgärden två markörer: Upprepa (ej synlig som en separat tagg) och Sluta upprepa. Placera de åtgärder du vill upprepa mellan Upprepa och Sluta upprepa-markörerna och ange hur många gånger du vill att åtgärden ska upprepas. När genvägen körs upprepas de åtgärder som du har placerat mellan markörerna i en loop det antal gånger som du har angett.
Exempelvis gör en loop som innehåller en Vibrera enhet-åtgärd inställd på Upprepa 3 gånger så att din iOS-enhet vibrera tre gånger när genvägen körs.
Du kan placera ett obegränsat antal åtgärder i en loop. Åtgärderna körs i ordning (från överst på listan och nedåt) och när den sista åtgärden har körts börjar genvägen om igen på den första åtgärden och så vidare (det angivna antalet gånger).
Tips: Du kan placera Upprepa-åtgärder inuti varandra i genvägen: I genvägsredigeraren drar du en andra Upprepa-åtgärd så att den överlappar den undre delen av den första Upprepa-åtgärden. Den andra Upprepa-åtgärden är inbäddad (indragen) under den första Upprepa-åtgärden.
Om inmatningsflöde i en Upprepa-åtgärd
När du överför inmatning till en Upprepa-åtgärd överförs samma inmatning till början av loopen vid varje upprepning. Om en genväg till exempel innehåller en Text-åtgärd följt av en loop med en Läs upp text-åtgärd, läser genvägen upp samma text flera gånger.
För varje upprepning av en loop samlas den sista åtgärdens utmatning i en lista och den listan blir utmatningen för Upprepa-åtgärden som helhet.
En Upprepa-åtgärd som körs fyra gånger och innehåller en Hämta senaste bilder-åtgärd matar t.ex. ut den bild som sparades senast i Bilder varje gång loppen upprepas. När alla fyra upprepningar har körts samlas utmatningarna ihop och en lista med fyra identiska bilder skapas. De fyra bilderna blir utmatningen från Upprepa-åtgärden som helhet. Denna utmatning överförs efter loopen vidare till nästa åtgärd.
Du kan läsa mer om inmatning och utmatning i Hur fungerar genvägar?
Tips: Tryck på det tomma utrymmet till vänster om pluset (+) och minuset (-) för att ställa in antalet upprepningar för en variabel. Om du exempelvis lägger till Fråga vid körning-variabeln i Upprepa-parametern i Uppreoa-åtgärden, blir du ombedd att välja hur många gånger loopen ska upprepas när genvägen körs.
Använda åtgärden Upprepa med varje
Åtgärden Upprepa med varje tar en lista med objekt som inmatning och kör sedan samma grupp av åtgärder en gång för varje objekt i listan. Det här är användbart om du arbetar med flera filer eller med flera innehållsdelar. Denna typ av loop upprepas tills varje objekt som överfördes till Upprepa med varje-åtgärd har körts.
Om du till exempel har ställt in en Hämta kommande aktiviteter-åtgärd för att hämta de fyra nästkommande Kalenderaktiviteterna och du placerar en Upprepa för varje-åtgärd efter åtgärden, körs loopen fyra gånger och varje upprepning tar emot en av kalenderaktiviteterna som inmatning.
När du drar en Upprepa för varje-åtgärd till din genväg innehåller åtgärden två markörer: Upprepa med varje (ej synlig som en separat tagg) och Sluta upprepa.
I slutet av loopen samlas all den utmatning som har överförts till Sluta upprepa-markören vid varje enskild upprepning ihop och överförs som utmatningen från Upprepa för varje-åtgärd som helhet.
Variabeln Upprepa objekt
Upprepa med varje-åtgärden erbjuder en särskild variabel för att spåra det aktuella objektet: Upprepa objekt. Variabeln Upprepa objekt uppdateras vid varje upprepning av loopen. Den innehåller det objekt som överfördes som inmatning för denna upprepning.
Om du till exempel överför 10 Kalenderobjekt till en Upprepa med varje-åtgärd, visar Upprepa objekt-variabeln en av Kalenderaktiviteterna i varje upprepning då genvägen kör genom varje kalenderobjekt.
Obs! Om du har placerat en Upprepa med varje-åtgärd inuti en annan Upprepa med varje-åtgärd, ändras variabelnamnet till Upprepa objekt 1 för att representera det inre upprepningsobjektet, Upprepa objekt 2 för den djupare loopen, och så vidare.
Variabeln Upprepa index
Alla loopar erbjuder en speciell variabel som heter Upprepa index. Den används för att hålla koll på den aktuella upprepningen. Variabeln Upprepa index innehåller det antal gånger en loop har upprepats hittills; den börjar vid 1 för den första upprepningen av loopen och ökar sedan med 1 för varje gång. Andra gången en loop körs är Upprepa index 2. Den tredje gången en loop körs är Upprepa index 3 och så vidare.
Obs! Om du har placerat en Upprepa-åtgärd inuti en annan Upprepa-åtgärd (skapat inbäddade upprepningsloopar), ändras den innersta Upprepa-åtgärdens variabelnamn till Upprepa index 2. En ytterligare inbäddad Upprepa-åtgärd skulle ge en Upprepa index 3-variabel och så vidare.
Mer information finns i Använda variabler i Genvägar.
Skapa oändliga loopar
Även om Genvägar inte erbjuder en åtgärd för att köra en grupp åtgärder för evigt, kan du efterlikna detta beteende genom att använda åtgärden Kör genväg. Med hjälp av åtgärden Kör genväg kan du köra en genväg inuti en annan. Konfigurera åtgärden Kör genväg för att köra samma genväg som åtgärden finns i. Genvägen körs då från början till slut ända tills du stoppar genvägen.
Testa utmatningen
Medan du lär dig att arbeta med åtgärderna Upprepa och Upprepa med varje, kan det vara till hjälp att testa resultaten som produceras efter Avsluta upprepa-markören. Detta för att säkerställa att innehållet är det du avsåg. Placera en överblicka-åtgärd efter Sluta upprepa-markören för att förhandsvisa de objekt som loopen matar ut.
Du kan också placera en Visa påminnelse-åtgärd efter Sluta upprepa-markörer och sätt in en Upprepa resultat magisk variabel i meddelandefältet för att visa text som har matats ut av loopen. Om du testar en loop som är mitt i en lång genväg är det bra att använda en Visa påminnelse-åtgärd för att förhandsvisa resultat. Detta eftersom du kan inkludera en Avbryt-knapp i påminnelsen för att avsluta genvägen vid den punkten i loopen.