Posts

Show image in Flutter

Image
  Flutter apps can include both code and  assets  (sometimes called resources). An asset is a file that is bundled and deployed with your app and is accessible at runtime. Common types of assets include static data (for example, JSON files), configuration files, icons, and images (JPEG, WebP, GIF, animated WebP/GIF, PNG, BMP, and WBMP). Load image from Asset Image . asset ( "assets/images/blogcover.jpeg" ,               height: 150 , width: 150 , fit: BoxFit .cover) Crashed????? Yes you have to add an image on  pubspec.yaml  file for more detail visit:  https://flutter.dev/docs/development/ui/assets-and-images Load image from URL Image . network ( 'http://www......png' , height: 150 , width: 150 , fit: BoxFit .cover), To set image round we have multiple options   CircleAvatar with border CircleAvatar ( radius: 55 , //for border backgroundColor: Colors.red , //for border width c

CustomStringConvertible in Swift

Image
Apple says: " A type with a customized textual representation.  Types that conform to the CustomStringConvertible protocol can provide their own representation to be used when converting an instance to a string. The String(describing:) initializer is the preferred way to convert an instance of any type to a string " CustomStringConvertible is a protocol and it's used print informative data of your object. Ok So let's start class Employee{ let name: String let surname: String init(name: String, surname: String) { self.name = name self.surname = surname } } let emp = Employee(name: "Pawan", surname: "Manjani") print(emp) So here if you print your class instance then it will print like below <YourProjectName.Employee: 0x60000152b240> So what we see here is not very informative, So if you want to print a readable data over here so here CustomStringConvertible  will help yo

Defer in Swift

Image
Defer is another new keyword in Swift. With this statement you can declare clean-up code, that is executed just before the current scope ends. Defer keyword was introduced in Swift 2.0 Defer is use to clean up operations like closing any Database connection, freeing up resources, invalidating the login session, etc. Syntax of Defer defer { statement 1 statement 2 ....... } Example of Defer func sayHello() { defer { print("World") } print("Hello") } sayHello() //It will prints "Hello" "World" Example of Defer with a return type function class Counter { var count = 0 func returnWithIncrement() -> Int { defer { count += 1 } print(count) return count } } let counter = Counter() counter.returnWithIncrement() // Print "0" counter.returnWithIncrement() // Print "1" Can we use multiple defer blocks in one function?? YE

Property wrapper in Swift

Image
Property wrapper is the Swift language feature that allows us to define a custom type, that implements behavior from get and set methods, and reuse it everywhere. Declare  @propertyWrapper  keyword before we declare the type that we want to use as property wrapper. It can be  struct ,  class , or  enum . We are required to implement the  wrappedValue  property. Most of the time we declare custom  setter  and  getter  in this property. This property can be a  computed  or  stored  property. Example of @Properywrapper @propertyWrapper struct Capitalized { private(set) var text: String = "" //Declared to store the value var wrappedValue: String { get { return text } set { text = newValue.uppercased() } } } //Now use it struct User{ @Capitalized var userName: String } var user = User() user.userName = "Pawan" print(user.userName) //PAWAN Thanks for reading, Happy Coding  👨‍💻

DateFomatter in Swift

Image
Apple Says A formatter that converts between dates and their textual representations. In simple word It is a class that can take a Date, and output a String describing that time/date as its format instructions dictate. In other words, it has a few pretty useful built-in formats, and the capability to accept a custom date format string. It helps to convert date to string and also helps from string to date Example of Date to String let date : Date = Date () let dateFormatter = DateFormatter () dateFormatter . dateFormat = "MM/dd/yyyy" let todaysDate = dateFormatter . string ( from : date ) //Output will be 05/04/2019 Example of String to Date let dateFormatter = DateFormatter () dateFormatter . dateFormat = "MM/dd/yyyy" let dateObj = dateFormatter . dateFromString ( "05/04/2019" ) More formatter Sunday , Apr 07 , 2019 --> EEEE , MMM d , yyyy 07 / 04 / 2019 -

Frame vs bounds in Swift

Image
1 ,  Frame The  frame  of an  UIView  is the  rectangle , expressed as a location (x,y) and size (width, height) relative to the superview it is contained within. In short ,  a view's location and size using the  parent view's coordinate system 2 ,  Bounds The  bounds  of an  UIView  is the  rectangle , expressed as a location (x,y) and size (width, height) relative to its own coordinate system (0,0).   In short ,  a view's location and size using  its own coordinate system Example: Frame origin = ( 40 , 60 ) width = 80 height = 130 Bounds origin = ( 0 , 0 ) width = 80 height = 130 Thanks for reading, Happy Coding  👨‍💻