Questions tagged [for-in-loop]

'for-in' is a type of 'Fast enumeration', which is a language feature that allows you to efficiently and safely enumerate over the contents of a collection using a concise syntax.

for-in is a language feature that allows you to efficiently and safely enumerate over the contents of a collection using a concise syntax.

603 questions
132
votes
7 answers

Type casting in for-in loop

I have this for-in loop: for button in view.subviews { } Now I want button to be cast into a custom class so I can use its properties. I tried this: for button in view.subviews as AClass But it doesnt work and gives me an error:'AClass' does not…
Arbitur
  • 38,684
  • 22
  • 91
  • 128
108
votes
5 answers

Python for-in loop preceded by a variable

I saw some code like: foo = [x for x in bar if x.occupants > 1] What does this mean, and how does it work?
Greg Flynn
  • 1,694
  • 2
  • 14
  • 15
107
votes
10 answers

"var" or no "var" in JavaScript's "for-in" loop?

What's the correct way to write a for-in loop in JavaScript? The browser doesn't issue a complaint about either of the two approaches I show here. First, there is this approach where the iteration variable x is explicitly declared: for (var x in…
futlib
  • 8,258
  • 13
  • 40
  • 55
63
votes
8 answers

ESLint doesn't allow for in

I have an object currentValues= {hey:1212, git:1212, nmo:12121} and I use for in like this: for (const key in currentValues) { if (Object.prototype.hasOwnProperty.call(currentValues, key)) { yield put(setCurrentValue(key,…
RamAlx
  • 6,976
  • 23
  • 58
  • 106
56
votes
5 answers

Swift: How to use for-in loop with an optional?

What's the proper way to use for-in loop with an optional? Right now I always perform an optional binding before looping it. Are there other idioms? let optionalInt:[Int]? = [1, 2, 3] if let optionalInt = optionalInt { for i in optionalInt { …
Boon
  • 40,656
  • 60
  • 209
  • 315
55
votes
4 answers

When to use forEach(_:) instead of for in?

As documented in both Array and Dictionary forEach(_:) Instance methods: Calls the given closure on each element in the sequence in the same order as a for-in loop. Nevertheless, adapted from Sequence Overview: A sequence is a list of values…
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
49
votes
3 answers

Can I use 'where' inside a for-loop in swift?

Is there also a possibility to use the 'where' keyword in another place then a switch? Can I use it in a for in loop for example? I have an array with bools, all with a value, can I do something like this: var boolArray: [Bool] = [] //(...) set…
Simon
  • 2,419
  • 2
  • 18
  • 30
38
votes
1 answer

How to skip to next in javascript in a for-in with a while inside?

I have a short javascript code where I need to skip to next in the for loop....see below: var y = new Array ('1', '2', '3', '4'); for (var x in y) { callFunctionOne(y[x]); while (condition){ condition = callFunctionTwo(y[x]); …
Ram Iyer
  • 1,404
  • 2
  • 20
  • 27
36
votes
1 answer

Can I use continue and break in Javascript for...in and for...of loops?

Can I use the break and continue statements inside the for...in and for...of type of loops? Or are they only accessible inside regular for loops. Example: myObject = { propA: 'foo', propB: 'bar' }; for (let propName in myObject) { if…
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
33
votes
1 answer

For...in statement Objective-C

I am studying Objective-C and I came across this "for...in" statement. I searched for it but i still don't get how it works. Could someone explain to me in a noob-friendly how this statement works?
Augusto Dias Noronha
  • 854
  • 2
  • 11
  • 20
33
votes
5 answers

JavaScript: JSLint error "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype"

I'm using the JSLint tool to ensure my JavaScript is "strict". I'm receiving the following error but don't understand how to fix it: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype For the…
HeatherK
  • 2,293
  • 4
  • 20
  • 12
26
votes
3 answers

For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift

I have a data model which I made for API returns, it is something like this: struct VehicleData: Codable { let _embedded: Embedded } struct Embedded: Codable { let userVehicles: [UserVehicles] } struct UserVehicles: Codable { …
XYD
  • 591
  • 1
  • 5
  • 9
26
votes
7 answers

Type Int does not conform to protocol sequence

I have the following code in Swift 3: var numbers = [1,2,1] for number in numbers.count - 1 { // error if numbers[number] < numbers[number + 1] { print(number) } } I am checking if the value on the index [number] is always higher…
Toby V.
  • 425
  • 1
  • 6
  • 15
22
votes
3 answers

Swift for-in loop dictionary experiment

I'm almost a complete programming beginner and I've started to go through a Swift ebook from Apple. The things I read are pretty clear, but once you start to experiment things get tricky :). I'm stuck with the experiment in the Control Flow section.…
al_x13
  • 305
  • 1
  • 3
  • 7
16
votes
3 answers

Swift - Unwrap optional in for in loop with where clause

I have a class with an optional member : class A { var i: Int? = nil } Then I have an array of objects of type A. Some objects in the array have a value for i, some others don't. I want to iterate over objects in the array that have a value…
Vince
  • 525
  • 1
  • 3
  • 19
1
2 3
40 41