SwiftUI vs UIKit
Apple'ın modern deklaratif framework'ü SwiftUI ile battle-tested UIKit arasındaki kapsamlı karşılaştırma. 2025'te hangi framework'ü seçmelisiniz?
Apple'ın güvenli, hızlı ve modern dili
JetBrains'in modern, güvenli ve pragmatik JVM dili
// Swift - Modern eşzamanlı veri yükleme
import Foundation
// Actor ile thread-safe data manager
actor UserDataManager {
private var cache: [String: User] = [:]
func fetchUser(id: String) async throws -> User {
if let cached = cache[id] {
return cached
}
let url = URL(string: "https://api.example.com/users/\(id)")!
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw APIError.invalidResponse
}
let user = try JSONDecoder().decode(User.self, from: data)
cache[id] = user
return user
}
}
// Kullanım
struct User: Codable {
let id: String
let name: String
let email: String
}
enum APIError: Error {
case invalidResponse
case notFound
}
// SwiftUI View içinde
@Observable class ProfileViewModel {
var user: User?
var isLoading = false
var errorMessage: String?
private let manager = UserDataManager()
func loadUser(id: String) async {
isLoading = true
defer { isLoading = false }
do {
user = try await manager.fetchUser(id: id)
} catch {
errorMessage = error.localizedDescription
}
}
}// Kotlin - Modern eşzamanlı veri yükleme
import kotlinx.coroutines.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
@Serializable
data class User(
val id: String,
val name: String,
val email: String
)
sealed class Result<out T> {
data class Success<T>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
}
class UserRepository(private val httpClient: HttpClient) {
private val cache = mutableMapOf<String, User>()
suspend fun fetchUser(id: String): Result<User> {
cache[id]?.let { return Result.Success(it) }
return try {
val user = httpClient.get<User>("https://api.example.com/users/$id")
cache[id] = user
Result.Success(user)
} catch (e: Exception) {
Result.Error(e)
}
}
}
// ViewModel (Android)
class ProfileViewModel(
private val repository: UserRepository
) : ViewModel() {
private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
fun loadUser(id: String) {
viewModelScope.launch {
_uiState.value = UiState.Loading
_uiState.value = when (val result = repository.fetchUser(id)) {
is Result.Success -> UiState.Success(result.data)
is Result.Error -> UiState.Error(result.exception.message ?: "Bilinmeyen hata")
}
}
}
sealed class UiState {
object Loading : UiState()
data class Success(val user: User) : UiState()
data class Error(val message: String) : UiState()
}
}Swift ve Kotlin son derece benzer modern dil özellikleri sunar — her ikisi de null safety, coroutine/async-await, extension'lar ve sealed class'lara sahip. Platform seçimi dili belirler: iOS için Swift zorunlu, Android için Kotlin standart. Kotlin Multiplatform, iki dünyayı birleştirme konusunda ilginç bir seçenek sunuyor.
Çok benzer. Her ikisi de type inference, extension, lambda, data/value types, null safety gibi özelliklere sahip. Biri öğrenilince diğeri çok daha kolay geliyor.