<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gayatri Hedau on Medium]]></title>
        <description><![CDATA[Stories by Gayatri Hedau on Medium]]></description>
        <link>https://medium.com/@gayatri.hedau?source=rss-544b12e70824------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BcCq5scbEn6iw4_2--e6IQ@2x.jpeg</url>
            <title>Stories by Gayatri Hedau on Medium</title>
            <link>https://medium.com/@gayatri.hedau?source=rss-544b12e70824------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 19:49:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gayatri.hedau/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[In App Purchase iOS Swift (IAP)]]></title>
            <link>https://medium.com/@gayatri.hedau/in-app-purchase-ios-swift-iap-c3a70a691e68?source=rss-544b12e70824------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3a70a691e68</guid>
            <category><![CDATA[iap]]></category>
            <category><![CDATA[in-app-purchase]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift-4]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Gayatri Hedau]]></dc:creator>
            <pubDate>Mon, 10 Jun 2019 13:30:26 GMT</pubDate>
            <atom:updated>2019-06-10T13:30:26.357Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*8euPBFubKYm8Yuj3BC-yEQ.jpeg" /></figure><p>An <strong>in-app purchase</strong> ( <strong>IAP</strong>) allows developers to charge users for specific functionality or content inside an application. An in-app purchase is a micro-transaction required by an app or game to purchase additional content after it has been downloaded. IAPs are a source of controversy since some users claim they ruin the experience of mobile gaming. Lets start with implementation for IPA.</p><h3><strong>1. Settings</strong></h3><p># 1. Go to your project -&gt; Target-&gt;Capabilities-&gt; In-App Purchase <br> Please make sure , in app purchase is enabled as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFeAYm8-jFo1lj-uz0YAnw.png" /></figure><p># 2. The next requirement to add in app purchase is , Apple Developer Account which is must . Create a new app on iTunes first to add an IAP. <br>Then go to My Apps -&gt; Features -&gt; In-App Purchase <br>Click to add new IAP, and select one of the following IAP type depends on your project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K8lg2q4gfhJ0p2xU4RUZOA.png" /></figure><p># 3. Fill the all required details as per shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21_d6YESl776ndlaz2cqCw.png" /><figcaption>Add your IAP name, Product ID (should be unique , need to use in code), and select pricing</figcaption></figure><p># 4. Save your product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-c6KmSoLj18pQZvjPo6bzA.png" /><figcaption>Explain about your product and add review screenshot</figcaption></figure><p># 5. Check your added product as like below , your product successfully added to IAP</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_vzHLjsedCZDJkI4P1PdSg.png" /></figure><h3>2. Implementation</h3><ol><li>Import StoreKit</li></ol><pre>import StoreKit</pre><p>2. Add delegate to ViewController</p><pre>&lt;SKProductsRequestDelegate,SKPaymentTransactionObserver&gt;</pre><p>3. Define Variables</p><pre>var defaultQueue = SKPaymentQueue()</pre><pre>var product = SKProduct(</pre><p>4. Write function for IAP</p><p>“productIdaddedOnitunes” this is the product ID which you have added while creating product on iTunes.</p><pre>// Initialiing payment <br>func callInAppPurchase() {</pre><pre>defaultQueue = SKPaymentQueue.default()</pre><pre>defaultQueue.add(self)</pre><pre>requestProductInfo(productId: &quot;productIdaddedOnitunes&quot;)</pre><pre>}</pre><pre>// Requesting product info from iTunes account </pre><pre>func requestProductInfo(productId: String) {</pre><pre>if SKPaymentQueue.canMakePayments() {</pre><pre>var productID = [productId]</pre><pre>let productIdentifiers = NSSet(array: productID)</pre><pre>let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as Set&lt;NSObject&gt; as Set&lt;NSObject&gt; as! Set&lt;String&gt;)</pre><pre>productRequest.delegate = self</pre><pre>productRequest.start()</pre><pre>SVProgressHUD.show()// Your activity indicator </pre><pre>} else {</pre><pre>print(&quot;No product found&quot;)</pre><pre>}</pre><pre>}</pre><pre>//Requesting product payment </pre><pre>func callPaymentInApp() {</pre><pre>var payment = SKPayment(product: product)</pre><pre>defaultQueue.add(payment)</pre><pre>//  C.activityIndicator.startAnimating()</pre><pre>}</pre><p>5. Delegate methods for SKProductsRequestDelegate</p><pre>// delegate method to check product availibilty with identifier </pre><pre>func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {</pre><pre>let products = response.products</pre><pre>if products.count != 0 {</pre><pre>product = products[0]</pre><pre>print(&quot;product Title = \(product.localizedTitle)&quot;)</pre><pre>print(&quot;product Description = \(product.localizedDescription)&quot;)</pre><pre>SVProgressHUD.show()</pre><pre>callPaymentInApp() // finally call payment request </pre><pre>} else if response.invalidProductIdentifiers.count != 0 {</pre><pre>print(&quot;\(response.invalidProductIdentifiers.description)&quot;)</pre><pre>SVProgressHUD.dismiss()</pre><pre>}</pre><pre>}</pre><pre>// delegate method to get transaction status of payment</pre><pre>func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {</pre><pre>for transaction: SKPaymentTransaction? in transactions as? [SKPaymentTransaction?] ?? [] {</pre><pre>switch transaction?.transactionState {</pre><pre>case .purchased?:</pre><pre>if let transaction = transaction {</pre><pre>defaultQueue.finishTransaction(transaction)</pre><pre>print(&quot;Success&quot;)</pre><pre>print(&quot;Transaction identifier - \(transaction.transactionIdentifier)&quot;)</pre><pre>print(&quot;Transaction date - \(transaction.transactionDate)&quot;)</pre><pre>SVProgressHUD.dismiss()</pre><pre>}</pre><pre>case .failed?:</pre><pre>if let transaction = transaction {</pre><pre>defaultQueue.finishTransaction(transaction)</pre><pre>SVProgressHUD.dismiss()</pre><pre>print(&quot;Failed&quot;)</pre><pre>}</pre><pre>case .restored?:</pre><pre>if let transaction = transaction {</pre><pre>defaultQueue.finishTransaction(transaction)</pre><pre>print(&quot;restored&quot;)</pre><pre>SVProgressHUD.dismiss()</pre><pre>}</pre><pre>default:</pre><pre>break</pre><pre>}</pre><pre>}</pre><pre>}</pre><p>6. Done with your IAP, check it , you will get pop up to allow your transaction by In App purchase .</p><p>Thank You.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AJaNx_n6nAjuOU4rbHtozw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3a70a691e68" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MKMapView with MKPolyLine in Swift]]></title>
            <link>https://medium.com/@gayatri.hedau/mkmapview-with-mkpolyline-in-swift-8b2779d29225?source=rss-544b12e70824------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b2779d29225</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[polyline]]></category>
            <category><![CDATA[mkmapview]]></category>
            <category><![CDATA[mapkit]]></category>
            <dc:creator><![CDATA[Gayatri Hedau]]></dc:creator>
            <pubDate>Sat, 12 Jan 2019 13:58:04 GMT</pubDate>
            <atom:updated>2019-01-12T13:58:04.208Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T97j5V-ALfBy314371EdiA.jpeg" /></figure><blockquote>The MapKit is a Framework based on the Apple Maps data and APIs and provides iOS developers with mechanism for integrating detailed mapping capabilities into any iOS application. <br>MKMapView class is the core element of MapKit Framework which provides the mechanism for showing maps and satellite to the use. MKPolyLine is a class which holds multiple location cor-ordinates and defines the shape of polyline .</blockquote><p><em>So let’s start with MKMapView….</em></p><p><strong>Step 1.</strong></p><p><em>Adding MKMapView on ViewController.swift and connecting outlet..</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uw9T5zKzkLGg9v9m4ONKdg.png" /></figure><p><strong>Step 2.</strong></p><p><em>Import MKMapKit and CoreLocation Framework</em></p><pre>import MapKit<br>import CoreLocation</pre><p><strong>Step 3.</strong></p><p><em>Add CLLocationManagerDelegate and MKMapViewDelegate delegates</em></p><pre>CLLocationManagerDelegate,MKMapViewDelegate</pre><p><em>Create LocationManager and CLLocationCoordinate2D variables</em></p><pre>var locationManager = CLLocationManager()<br>var testcoords:[CLLocationCoordinate2D] = []</pre><p><strong>Step 4.</strong></p><p><em>Set up LocationManager and MapView</em></p><pre>func setUp() {<br>// location manager</pre><pre>locationManager = CLLocationManager()<br>locationManager.delegate = self<br>locationManager.desiredAccuracy = kCLLocationAccuracyBest<br>locationManager.requestAlwaysAuthorization()</pre><pre>//Map view settings<br>customMkMapV.delegate = self<br>customMkMapV.mapType = MKMapType.standard<br>customMkMapV.isZoomEnabled = true<br>customMkMapV.isScrollEnabled = true<br>customMkMapV.center = view.center</pre><pre>// adding co-ordinates for poly line (added static, we can make it dyanamic)<br>let coords1 = CLLocationCoordinate2D(latitude: 21.1233668, longitude: 79.1027889)<br>let coords2 = CLLocationCoordinate2D(latitude: 21.122083, longitude: 79.1135274)<br>let coords3 = CLLocationCoordinate2D(latitude: 21.1235418, longitude: 79.1150327)<br>let coords4 = CLLocationCoordinate2D(latitude: 21.1384636, longitude: 79.1189755)</pre><pre>testcoords = [coords1,coords2,coords3,coords4]</pre><pre>determineCurrentLocation() // updating current location method</pre><pre>}</pre><p><strong>Step 5.</strong></p><p><em>Update to current location</em></p><pre>func determineCurrentLocation() {<br>if CLLocationManager.locationServicesEnabled() {<br>locationManager.startUpdatingLocation()<br>} <br>}</pre><p><strong>Step 6.</strong></p><p>Most important thing is to add delegate methods of MKMapView in which we are showing Map region and Polyline</p><pre>func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {</pre><pre>// adding map region<br>let userLocation:CLLocation = locations[0] as CLLocation</pre><pre>let testline = MKPolyline(coordinates: testcoords, count: testcoords.count)</pre><pre>let center = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)</pre><pre>let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))<br></pre><pre>// adding annotation aat particular point</pre><pre>for each in 0..&lt;testcoords.count{<br>let anno = MKPointAnnotation()<br>anno.coordinate = testcoords[each]<br>customMkMapV.addAnnotation(anno as MKAnnotation)<br>}</pre><pre>customMkMapV.setRegion(region, animated: true)<br>customMkMapV.addOverlay(testline)<br>locationManager.stopUpdatingLocation()</pre><pre>}</pre><p>Adding polyline</p><pre>func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -&gt; MKOverlayRenderer {</pre><pre>if let polyline = overlay as? MKPolyline {<br>let testlineRenderer = MKPolylineRenderer(polyline: polyline)<br>testlineRenderer.strokeColor = .blue<br>testlineRenderer.lineWidth = 2.0<br>return testlineRenderer<br>}</pre><pre>}</pre><p>Add Permission for location access in Info.plist</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HX5pkbiv8fxh3zcJlYiEqQ.png" /></figure><p>Open Info.plist as source code and add below lines</p><pre>&lt;key&gt;NSLocationAlwaysUsageDescription&lt;/key&gt;<br>&lt;string&gt;Program requires GPS to track location&lt;/string&gt;&lt;key&gt;NSLocationAlwaysAndWhenInUseUsageDescription&lt;/key&gt;&lt;string&gt;Program requires GPS to track location&lt;/string&gt;&lt;key&gt;NSLocationWhenInUseUsageDescription&lt;/key&gt;<br>&lt;string&gt;Program requires GPS to track location&lt;/string&gt;</pre><p><em>Finally call</em> setUp()<em> method from </em>viewDidLoad()<br><em>Done..<br>Run the application and check for output…..</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*DZ9niVX8rAT2Qt0CS1Nnug.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*CV0hgoPZ1ViHgwco6CkgaQ.jpeg" /><figcaption>Allow permission for location access and check MKPolyLine</figcaption></figure><p>Thank You…!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b2779d29225" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[QR Code Scanner with AVCaptureSession iOS Swift]]></title>
            <link>https://medium.com/@gayatri.hedau/qr-code-scanner-with-avcapturesession-ios-swift-5007ee498b49?source=rss-544b12e70824------2</link>
            <guid isPermaLink="false">https://medium.com/p/5007ee498b49</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[qr-code]]></category>
            <dc:creator><![CDATA[Gayatri Hedau]]></dc:creator>
            <pubDate>Fri, 04 Jan 2019 16:10:06 GMT</pubDate>
            <atom:updated>2019-01-04T16:10:06.428Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*QQ8CvlyD1TOBmS6zsR2BRw.jpeg" /><figcaption>Scan QR Code</figcaption></figure><blockquote>This is the demo app where we are going to use QR code scanner with AVCaptureSession. We are using AVFoundation framework which is basically used for video capturing and scanning QR code is exactly based on video capturing.</blockquote><blockquote>So, this is the starting point to import AVFoundation framework in your application. Let’s get start.</blockquote><p><strong>#Step 1:</strong></p><p>Create a new project with application type “Single View App” and first import AVFoundation framework into ViewController.Swift</p><pre><strong>import</strong> AVFoundation</pre><p><strong>#Step 2:</strong></p><p>Add AVCaptureMetadataOutputObjectsDelegate in ViewController class.</p><pre>AVCaptureMetadataOutputObjectsDelegate</pre><p><strong>#Step 3:</strong></p><pre><strong>var</strong> captureSession: AVCaptureSession!<br><strong>var</strong> previewLayer: AVCaptureVideoPreviewLayer!</pre><p><strong>#Step 4:</strong></p><p>Call method from viewDidLoad().</p><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br><strong>super</strong>.viewDidLoad()</pre><pre>view.backgroundColor = UIColor.black<br>self.getCameraPreview()</pre><pre>}</pre><p><strong>#Step 5:</strong></p><pre>func getCameraPreview(){</pre><pre>captureSession = AVCaptureSession()<br><strong>guard</strong> <strong>let</strong> videoCaptureDevice = AVCaptureDevice.default(for: .video) <strong>else</strong> { <strong>return</strong> }</pre><pre><strong>let</strong> videoInput: AVCaptureDeviceInput</pre><pre><strong>do</strong> {<br>videoInput = <strong>try</strong> AVCaptureDeviceInput(device: videoCaptureDevice)<br>} <strong>catch</strong> {<br><strong>return<br></strong>}</pre><pre><strong>if</strong> (captureSession.canAddInput(videoInput)){<br>captureSession.addInput(videoInput)<br>} <strong>else</strong> {<br>showAlert()<br><strong>return<br></strong>}</pre><pre><strong>let</strong> metadataOutput = AVCaptureMetadataOutput()</pre><pre><strong>if</strong> (captureSession.canAddOutput(metadataOutput)) {<br>captureSession.addOutput(metadataOutput)<br>metadataOutput.setMetadataObjectsDelegate(<strong>self</strong>, queue: DispatchQueue.main)<br>metadataOutput.metadataObjectTypes = [.qr]<br>} <strong>else</strong> {<br>showAlert()<br><strong>return<br></strong>}</pre><pre>previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)<br>previewLayer.frame = view.layer.bounds<br>previewLayer.videoGravity = .resizeAspectFill<br>view.layer.addSublayer(previewLayer) // add preview layer to your view </pre><pre>captureSession.startRunning() // start capturing<br>}</pre><p><strong>#Step 6:</strong></p><pre><strong>func</strong> showAlert() {</pre><pre><strong>let</strong> ac = UIAlertController(title: &quot;Scanning not supported&quot;, message: &quot;Your device doesn&#39;t support for scanning a QR code. Please use a device with a camera.&quot;, preferredStyle: .alert)</pre><pre>ac.addAction(UIAlertAction(title: &quot;OK&quot;, style: .default))<br>present(ac, animated: <strong>true</strong>)<br>captureSession = <strong>nil</strong></pre><pre>}</pre><p><strong>#Step 7:</strong></p><p>Add method for getting metadata output after scanning finished.</p><pre><strong>func</strong> metadataOutput(<strong>_</strong> output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {</pre><pre>captureSession.stopRunning() // stop scanning after receiving metadata output</pre><pre><strong>if</strong> <strong>let</strong> metadataObject = metadataObjects.first {<br><strong>guard</strong> <strong>let</strong> readableObject = metadataObject <strong>as</strong>? AVMetadataMachineReadableCodeObject <strong>else</strong> { <strong>return</strong> }</pre><pre><strong>guard</strong> <strong>let</strong> codeString = readableObject.stringValue <strong>else</strong> { <strong>return</strong> }<br>AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))</pre><pre>self.receivedCode(qrcode: codeString)</pre><pre>}</pre><pre>}</pre><p>Here, you will get a QR code data in “codeString” variable, same will show in alert below.</p><p><strong>#Step 8:</strong></p><pre><strong>func</strong> receivedCode(qrcode: String) {</pre><pre>print(qrcode)</pre><pre><strong>let</strong> alertController = UIAlertController(title: &quot;Success&quot;, message: qrcode, preferredStyle: .alert)</pre><pre><strong>let</strong> action1 = UIAlertAction(title: &quot;Ok&quot;, style: .default) { (action:UIAlertAction) <strong>in</strong></pre><pre><strong>self</strong>.dismiss(animated: <strong>true</strong>)</pre><pre>}<br>alertController.addAction(action1)<br><strong>self</strong>.present(alertController, animated: <strong>true</strong>, completion: <strong>nil</strong>)</pre><pre>}</pre><p>and the output is ……..</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/179/1*xFxa7XNgXrtWc-VauWxVPA.png" /><figcaption>Scanned QR Code</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5007ee498b49" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Core Data iOS Swift]]></title>
            <link>https://medium.com/@gayatri.hedau/core-data-ios-swift-ed66b2e700fc?source=rss-544b12e70824------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed66b2e700fc</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[core-data]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Gayatri Hedau]]></dc:creator>
            <pubDate>Tue, 01 Jan 2019 07:32:52 GMT</pubDate>
            <atom:updated>2019-01-01T07:32:52.791Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*ZJVmom-VIx-qL2khVmeImQ.png" /></figure><blockquote>Core Data is one of the most popular framework provided by Apple for iOS applications. It is not a database, its a framework to save, retrieve, modify and filter data from iOS apps. Let’s start with basics methods to save, retrieve , update, and delete data from iOS apps.</blockquote><ol><li>Create a project with application type “Single View App” and make sure that you have checked “Use Core Data”</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*JxeaOSI-6kcjubKZcuDDXg.png" /></figure><p>2. After creating project you will get “CoreData.xcdatamodeld” which we are going to create model entity and other purpose.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*WKGqvu1yv16eVHtBws4IdA.png" /></figure><p>3. Click on CoreData.xcdatamodeld to create an entity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_cuQeU3TFgMhBZ0d2yBonA.png" /></figure><p>Click on “Add Entity” button to create entity.</p><p>4. Add attributes</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EdHMdtTgPNs2yNTyMcLVow.png" /></figure><p>Now, we have Entity as well as attributes , so let’s start with adding data to Core Data.</p><h4># Add Data to Core Data</h4><pre>func saveData {</pre><pre>guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return}</pre><pre>// 1<br>let managedContext = appDelegate.persistentContainer.viewContext</pre><pre>// 2 <br>let entity = NSEntityDescription.entity(forEntityName: “YourEntityName”, in: managedContext)!<br>let person = NSManagedObject(entity: entity,insertInto: managedContext)</pre><pre>// 3<br>person.setValue(nameValue, forKeyPath: “name”)<br>person.setValue(ageValue, forKeyPath: “age”)</pre><pre>// 4<br>do {<br>try managedContext.save()<br>people.append(person)</pre><pre>let alert = UIAlertController(title: “Success”, message: “Saved Successfully”, preferredStyle: UIAlertController.Style.alert)</pre><pre>alert.addAction(UIAlertAction(title: “Ok”, style: UIAlertAction.Style.default, handler: nil ))</pre><pre>self.present(alert, animated: true, completion: nil)</pre><pre>} catch let error as NSError {<br>// print(“Could not save. \(error), \(error.userInfo)”)}<br>}</pre><h4># Retrive data from Core Data</h4><pre>func getData() {</pre><pre>var people: [NSManagedObject] = []</pre><pre>//1<br>guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {<br>return<br>}</pre><pre>let managedContext = appDelegate.persistentContainer.viewContext</pre><pre>//2 <br>let fetchRequest = NSFetchRequest&lt;NSManagedObject&gt;(entityName: &quot;YourEntityName&quot;)</pre><pre>//3<br>do {<br>people = try managedContext.fetch(fetchRequest)</pre><pre>//  print(&quot;**************** FETCHED REQUEST DATA *************&quot;<br>//  print(people)</pre><pre>} catch let error as NSError {<br>//   print(&quot;Could not fetch. \(error), \(error.userInfo)&quot;<br>}<br>}</pre><h4># Delete Particulars from Core Data</h4><pre>func deleteParticulars(name: String) {</pre><pre>// 1<br>guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }</pre><pre>let managedContext = appDelegate.persistentContainer.viewContext</pre><pre>// 2<br>let fetchrequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: &quot;YourEntityName&quot;)<br>fetchrequest.predicate = NSPredicate(format: &quot;name = %@&quot;, name)</pre><pre>// 3 <br>do {<br>let test = try managedContext.fetch(fetchrequest)<br>let objectToDelete = test[0] as! NSManagedObject<br>managedContext.delete(objectToDelete)</pre><pre>  do {<br>       try managedContext.save()<br>       //   print(&quot;deleted successfully&quot;)<br>     } catch  {<br>       //  print(error)<br>     }<br>} catch {<br>//  print(error)<br>}</pre><pre>}</pre><h4># Update data from Core Data</h4><pre>func updateData(name: String, age: String) {</pre><pre>// 1<br>guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }</pre><pre>let managedContext = appDelegate.persistentContainer.viewContext</pre><pre>// 2 <br>let fetchRequest:NSFetchRequest&lt;NSFetchRequestResult&gt; = NSFetchRequest.init(entityName: &quot;Questiontable&quot;)</pre><pre><br>// 3<br>let fetchrequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: &quot;YourEntityName&quot;)</pre><pre>fetchrequest.predicate = NSPredicate(format: &quot;name = %@&quot;, name)</pre><pre>do<br>{ <br>let test = try managedContext.fetch(fetchrequest)<br>let objectUpdate = test[0] as! NSManagedObject</pre><pre>objectUpdate.setValue(newNameValue, forKey: &quot;name&quot;)<br>objectUpdate.setValue(newAgeValue, forKey: &quot;age&quot;)<br></pre><pre>   do {<br>        try managedContext.save()<br>        let alert = UIAlertController(title: &quot;Success&quot;, message:               <br>&quot;Updated Successfully&quot;, preferredStyle: UIAlertController.Style.alert)</pre><pre>alert.addAction(UIAlertAction(title: &quot;Ok&quot;, style: UIAlertAction.Style.default, handler: nil))</pre><pre>self.present(alert, animated: true, completion: nil)<br>}<br>   catch<br>{<br>print(error)<br>}<br>}</pre><pre>catch<br>{<br>print(error)<br>}<br>}</pre><p>So, that’s it for Core Data.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed66b2e700fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>