iMac longevity for iOS development
...Because the older the cheaper


This goes without saying!


  How long a 2012 iMac can be used for iOS development? Or in more general sense, how long an iMac can be used for iOS development?


Nobody can really answer this because nobody can know for sure when any particular model Mac will no longer be supported.  In fact, this article goes into detail, but their very first words are "Apple has no written policy"

IMO, the 2012 is probably not the best choice because it's at the "cutoff" for supported iMacs on the macOS Catalina release.  

Will it...


be supported by the next version?  Nobody knows.  
still receive updates?  Most likely.
run the version of XCode required to develop on the latest iDevices?  That's the kicker.  It may or may not.


Being that your at the very end of the line, I'd look for something about 3 years "younger."  This will buy you some time before you get a more modern, well supported machine.