Found 33676 Articles for Programming

Detect current device with UIUserInterfaceIdiom in Swift

Anvi Jain
Updated on 30-Jul-2019 22:30:24

3K+ Views

To detect current device with iOS/Swift we can use UserInterfaceIdiom. It is an enum in swift, which tells which device is being used.The interface idiom provides multiple values in it’s enum which are.case unspecified @available(iOS 3.2, *) case phone // iPhone and iPod touch style UI @available(iOS 3.2, *) case pad // iPad style UI @available(iOS 9.0, *) case tv // Apple TV style UI @available(iOS 9.0, *) case carPlay // CarPlay style UIIn swift interfaceIdiom can be used in the following way:print(UIDevice.current.userInterfaceIdiom) if UIDevice.current.userInterfaceIdiom == .phone { print("running on iPhone") }When we run the above code ... Read More

How to detect shake gesture using Swift?

Rishi Rathor
Updated on 29-Jun-2020 14:03:26

1K+ Views

To detect a shake gesture in iOS UIKit provides three different methods, let’s see them one by one.Method 1 − When the shake gesture begins.override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // code you want to implement }Method 2 − When the shake gesture ends.override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // Code you want to implement. }Method 3 − when the shake gesture is cancelled.override func motionCancelled(_ motion: UIEvent.EventSubtype, with event: UIEvent?) { // code you want to implement. }Now let’s add some code in our motionBegan method, override func motionBegan(_ motion: UIEvent.EventSubtype, with event: ... Read More

How to subtract 1 hour from current time using Swift?

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:24

3K+ Views

To subtract hours from a date in swift we need to create a date first. Once that date is created we have to subtract hours from that, though swift does not provide a way to subtract date or time, but it provides us a way to add date or date component in negative value. In this example we’ll see how we can achieve the same.Let’s create a date first, let it be today, let today = Date()Now to modify this date we’ll use the add function with negative value, let modifiedDate = Calendar.current.date(byAdding: .hour, value: -2, to: today)!Now to see ... Read More

Iterate through a LinkedList using an Iterator in Java

Chandu yadav
Updated on 29-Jun-2020 13:53:57

6K+ Views

An Iterator can be used to loop through an LinkedList. The method hasNext( ) returns true if there are more elements in LinkedList and false otherwise. The method next( ) returns the next element in the LinkedList and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("John");       l.add("Sara");       l.add("Susan");       l.add("Betty");       l.add("Nathan");   ... Read More

Use Iterator to remove an element from a Collection in Java

Arjun Thakur
Updated on 29-Jun-2020 13:54:59

19K+ Views

An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");       System.out.println("The ArrayList elements are: ");     ... Read More

Loop through a HashMap using an Iterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:55:50

251 Views

An Iterator can be used to loop through a HashMap. The method hasNext( ) returns true if there are more elements in HashMap and false otherwise. The method next( ) returns the next key element in the HashMap and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Demo {    public static void main(String[] args) {       Map student = new HashMap();       student.put("101", "Harry");       student.put("102", "Amy");       student.put("103", "John");       ... Read More

Loop through an ArrayList using an Iterator in Java

George John
Updated on 29-Jun-2020 13:56:42

4K+ Views

An Iterator can be used to loop through an ArrayList. The method hasNext( ) returns true if there are more elements in ArrayList and false otherwise. The method next( ) returns the next element in the ArrayList and throws the exception NoSuchElementException if there is no next element.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Mango");       aList.add("Guava");       aList.add("Orange");       aList.add("Peach");   ... Read More

Create an object array from elements of LinkedList in Java

Chandu yadav
Updated on 29-Jun-2020 13:45:11

446 Views

An object array can be created from the elements of a LinkedList using the method java.util.LinkedList.toArray(). This method returns the object array with all the LinkedList elements in the correct order.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; public class Demo {    public static void main(String[] args) {       LinkedList l = new LinkedList();       l.add("Amy");       l.add("Sara");       l.add("Joe");       l.add("Betty");       l.add("Nathan");       Object[] objArr = l.toArray();       System.out.println("The object array elements are: ");       for ... Read More

Iterate through elements of a LinkedList using a ListIterator in Java

Ankith Reddy
Updated on 29-Jun-2020 13:46:41

244 Views

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in a LinkedList.The method hasNext( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the forward direction and false otherwise. The method next( ) returns the next element in the LinkedList and advances the cursor position.The method hasPrevious( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the LinkedList and reduces the ... Read More

Replace an element in an ArrayList using the ListIterator in Java

George John
Updated on 29-Jun-2020 13:47:32

1K+ Views

An element in ArrayList can be replaced using the ListIterator method set(). This method has a single parameter i.e. the element that is to be replaced and the set() method replaces it with the last element returned by the next() or previous() methods.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo {    public static void main(String[] args) {       ArrayList aList = new ArrayList();       aList.add("Amanda");       aList.add("Taylor");       aList.add("Justin");       aList.add("Emma");       aList.add("Peter");       System.out.println("The ArrayList elements ... Read More

Advertisements