Post

Replies

Boosts

Views

Activity

Map Annotation cluster
I need to display a photo for a group of annotations (cluster) on a map. The photo should be selected randomly from the annotations that are inside that cluster. Currently, my method only works if I assign a static variable for 'pinToDisplay' in the 'AllPins' class. However, this causes a memory leak when a lot of pins are added, since it stores the photo from Firebase data. If I remove the static property from the variable, memory usage becomes more efficient, but the cluster is unable to display a photo for a group of annotations. It still works for a single annotation though. here the code class PinParsed: Codable { let createdDate, id, owner, updatedDate: String? let address: String? let approve: Bool? let category: [String]? let city: String? let country: String? let creator: String? let image: String? let img: String? let lat: String? let linkPinAll: String? let linkPinTitle, long: String? let subscrtag: [String]? let title: String? let gal: [GalClass]? let instagramLink, googleStreetViewURL: String? } class PinAnnotation : MKPointAnnotation { var pin: PinParsed! var image: String! } final class AllPins { static var pinToDisplay: [PinAnnotation]? deinit { print("allpins deinit") } //This is in the class which is responsible for cluster override var annotation: MKAnnotation? { didSet { guard let annotation = annotation as? MKClusterAnnotation else { return } countLabel.text = "\(annotation.memberAnnotations.count)" } willSet { guard let annotation = annotation as? MKClusterAnnotation else { return } var randomElement = annotation.memberAnnotations.randomElement() let latitude = randomElement?.coordinate.latitude let longitude = randomElement?.coordinate.longitude if latitude != nil, longitude != nil { if let allPins = AllPins.pinToDisplay { allPins.forEach { pinAnnotation in if pinAnnotation.coordinate.latitude == latitude && pinAnnotation.coordinate.longitude == longitude { if let url = URL(string: pinAnnotation.pin.img!) { URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in if let error = error { print(error.localizedDescription, "cluster photo") } if let imageData = data { DispatchQueue.main.async { self!.imageView.image = UIImage(data: imageData) self!.imageView.contentMode = .scaleAspectFill } } }.resume() } } } } } }
0
0
1k
Mar ’23
Fire local notification when dead line is come
In my to-do app, I'm trying to set local notification at the time when dead line date for task have been came, but i can't figure out whats wrong with calendar trigger, interval trigger working ok.    func setupNotifications(id: String, contentTitle: String, contentBody: String, date: Date?) {     center.getNotificationSettings { (settings) in       if (settings.authorizationStatus == .authorized) {         let content = UNMutableNotificationContent()         content.title = contentTitle         content.body = contentBody         content.sound = .default         let newdate = date?.addingTimeInterval(5)         let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .second], from: newdate!)         let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)         let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)         self.center.add(request)                }       }
0
0
531
Dec ’22
Reversing string with ignoring characters
Need to reverse strings without affecting characters which user can add by themself. For example i need to reverse abcd123 and ignore "b" and "3" so i need to get 2b1dca3. By ignoring i mean this characters should stay in place and do not swap with others. My code is working only with full word for example func reverseWithoutFilter(FullText:String, TextToIgnore:String) -> String { let fullTextArray = FullText.components(separatedBy: " ") let textToIgnoreArray = TextToIgnore.components(separatedBy: " ") let result = fullTextArray.map{!textToIgnoreArray.contains($0) ? String($0.reversed()) : $0} return result.joined(separator: " ") } var result = reverseWithoutFilter(FullText: "FOX is good", TextToIgnore: "FOX") // result will be "FOX si doog"
5
1
753
Jun ’22