Thursday, December 17, 2015

Converting Date to a String or String to a Date in Swift

A very common use case many of you will come across is converting a NSDate object to a String object or vice versa. When I came across a use case where I had to convert a Date to String in my iOS app written in Swift I did an online search but it was hard to find a good example of the correct solution so I hope to provide an easy reference for anyone who is looking to implement the same.

Scenario 1: Converting Date to String

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "Y/M/d"
let duedateString = dateFormatter.stringFromDate(dueDatePicker.date)

Note the format "Y/M/d" used in the second line above is a Date Format value that defines how the date is represented in the String format. You can find a good resource here for the various values you can use to define the date format.

Also note that the dueDatePicker.date value used in the last line of the code above is referencing a Date Picker in the UI of my app, however, you can simply replace it with any Date object.

Let's say the user picked 12/25/2015 as the date, the value of duedateString at the end of the code's execution will be "2015/12/25" because of the format we used which says give me a string in Y/M/d format.

Scenario 2: Converting String to Date

This is even simpler and requires only 2 lines of code.

let dateFormatter = NSDateFormatter()
let dueDate = dateFormatter.dateFromString("12/17/2015")

Hope this helps!

Regards,

Tuesday, November 24, 2015

The model used to open the store is incompatible with the one used to create the store

If you are working on an iOS app and have used Core Data in your app you might suddenly come across an error with any of these messages: "The model used to open the store is incompatible with the one used to create the store," "Failed to initialize the application's saved data," or "There was an error creating or loading the application's saved data."

Don't fret, this simply means some of the data model for your Core Data has changed, it could be something as simple as adding a new attribute to an entity in Core Data, or modifying the data type of an attribute. Luckily the solution is quite simple in most cases - you can simply delete the app from your device or simulator and install/run the app again and it should work fine.

Note how I said the solution is simple for most cases; that's because it is not always practical to delete the app before installing the new version - either because you are testing upgrade functionality that you cannot test if you uninstall the app, or because you want to launch the app in the App Store and can't really expect your users to uninstall your app before installing an update. That's a sure way to have your app permanently uninstalled from the user's device! Unfortunately in this case modifying items in Core Data is not as easy; you'll have to migrate your data which entails writing some code to take care of it. I'll cover this in a future post but the solution above should work for over 90% of your needs while in development.

Cheers,
Paras Wadehra
Microsoft MVP
My WP Apps
http://twitter.com/ParasWadehra
FB.com/MSFTDev

Thursday, December 18, 2014

Error pushing Node.js app to Heroku


If you are building a Node.js app and have been deploying your app to Heroku and testing it there all along, you might be surprised to see this error next time you try pushing your code to Heroku with the following command...

git push heroku master

The error you might see is:

Push rejected, failed to compile Node.js app

The full error may look like this:

Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 972 bytes | 0 bytes/s, done.
Total 9 (delta 4), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Removing .DS_Store files
remote: -----> Node.js app detected
remote: -----> Resetting git environment
remote: 
remote:  !     Push rejected, failed to compile Node.js app
remote: 
remote: Verifying deploy...
remote: 
remote: ! Push rejected to project.
remote: 
To https://git.heroku.com/project.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/project.git'

While this is a bit of a generic error and can be caused by several different issues, one of the reasons I face it is that the npm cache is stale, so a very easy way to resolve such an issue is by cleaning your npm cache simply by issuing this command:

npm cache clean

If you require administrative privileges to run this command, adding sudo in front of the command will help, like this...

sudo npm cache clean

Hope this helps...

Until next time,
Paras Wadehra
Microsoft MVP


Sunday, November 16, 2014

My first Microsoft MVP Summit experience

I got to attend my first Microsoft MVP Summit earlier this month in Redmond, Washington. This is a huge benefit of being a Microsoft MVP. Approximately 1,500 MVPs from around the world attended the summit this year, which is an invite-only event.

Microsoft invites its MVPs to not only share news about upcoming stuff from the Redmond mill, but also to gather feedback and hear about what the community expects to see from Microsoft - the MVPs being the ears and eyes of Microsoft around the world inside the community of users who use Microsoft products everyday.

This year, I hear, was bigger and better than the years past. Microsoft hosted a MVP showcase on Sun, Nov-2 where MVPs from different technology groups showcased the stuff they've been working on. There were even some Microsoft teams like the Surface, Azure and Office who were showcasing their products. The main sessions for the summit started on Monday. Microsoft provided buses to and from all the official summit hotels to the main campus and also shuttled around the MVPs from one campus building to another as different sessions were being held in different buildings. One of the biggest benefits of hosting the MVP Summit on the main Microsoft campus is that we get to meet all the different product teams and interact directly with them. Another big benefit is being able to meet and interact with other fellow MVPs, who are all smart and highly regarded people in their respective field.

If you didn't know already, I was awarded the MVP status earlier this year in what is now the Windows Platform Development expertise, which meant I attended the Windows Platform Development related sessions for the most part. While I cannot talk about what we discussed during the sessions, as it is all under NDA - yes, all MVPs have to sign a NDA (Non Disclosure Agreement) with Microsoft in order to be eligible to attend the summit as a lot of not-yet publicly known information is shared during the sessions - I can tell you that we all live in exciting times! Oh, and during one of the sessions I saw an image of myself popup on the screen which made me smile...check it out for yourself below, I am the second one from the left.

Microsoft using my photo in their presentations

The main sessions, side sessions and one-to-one feedback sessions continued till Thursday, but I don't want you to think that it was all talks and no fun. We started out on Sunday with a Product Group event where we all got to split into teams and participate in an The Amazing Race like city-wide fun-run in Seattle. Monday evening we had team dinners, though it left a lot to be desired from a dinner perspective and lot of us had to hunt for food elsewhere. All was redeemed on Tuesday evening though, when the CEO of Microsoft, Satya Nadella, had a private talk with the MVPs followed by dinner. Wednesday evening saw the official MVP Summit Attendee Party at Fremont Studios in Seattle which was a fun social event with enough food and drinks to cater to all along with some karaoke.

Alas, all good things come to an end and with the Summit sessions over on Thursday, it was time for me to get back home. But along with me I brought back memories of some great sessions and great conversations I had over the period of the last 4 days. I got to meet so many folks that I had only interacted with virtually over the Internet via Twitter, Email, LinkedIn, Microsoft forums, etc. before the summit.

Here's hoping that I get to live another day (get to attend another MVP Summit that is).

Regards,
Paras Wadehra
Microsoft MVP
Twitter: @ParasWadehra

Saturday, October 4, 2014

Returning multiple values from a method

During your programming escapades you are sure to come across an instance when you need to return multiple values from a method.

The traditional ways of doing it are:

(i) to create an object of a class, instantiate it and assign values to the object inside the method and then return the object from the method, or

(ii) to assign values to multiple global variables inside the method and then access them elsewhere, or

(iii) to use an out parameter to return the updated value back to the calling method, or

(iv) to use a ref parameter to modify the referenced parameter directly from inside the called method.


Well, there is another lesser-known way in .Net that allows you to return multiple values from a method without having to create an object or multiple variables, it is a feature called Tuple.

Here is some sample code in C# that shows you how to create and use a Tuple to return multiple values from a method:

public Tuple<int, string, string> GetEmployee()
{
    int employeeId = 23;
    string firstName = "John";
    string lastName = "Doe";

    //Create a tuple and return
    return Tuple.Create(employeeId, firstName, lastName);
}

Regards,
Paras Wadehra
Microsoft MVP
Twitter: @ParasWadehra

Thursday, August 21, 2014

How to send email from your Windows Phone 8.1 app

As part of the API changes made in the new Windows Phone 8.1 XAML programming model, the EmailComposeTask - and several other tasks - that were available in the Microsoft.Phone.Tasks namespace in Windows Phone 8 are no longer available.

Most of these have been replaced with a new way of performing the same tasks. For sending an email from within your app, the old way in Windows Phone 8 was as follows:

OLD WAY
========

EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "Subject";
emailComposeTask.To = "support@developer.com";
emailComposeTask.Show();


The new way in Windows Phone 8.1 to send an email from your app is as follows:

NEW WAY
=========

// Define Recipient
EmailRecipient sendTo = new EmailRecipient()
{
    Name = "Name of Recepient",
    Address = "support@developer.com"
};

// Create email object
EmailMessage mail = new EmailMessage();
mail.Subject = "this is the Subject";
mail.Body = "this is the Body";

// Add recipients to the mail object
mail.To.Add(sendTo);
//mail.Bcc.Add(sendTo);
//mail.CC.Add(sendTo);

// Open the share contract with Mail only:
await EmailManager.ShowComposeNewEmailAsync(mail);


Have fun,
Paras Wadehra
Microsoft MVP - Windows Platform Development
http://twitter.com/ParasWadehra
https://www.facebook.com/WindowsPhoneDeveloper
My WP Apps

Saturday, August 2, 2014

Xamarin Forms Shared project template error

The new Xamarin Forms Shared Project Template has a bug which causes the Windows Phone build to fail out of the box. You might see an error like:

The 'ProductID' attribute is invalid - The value 'b79569cb-1898-4dab-9173-afe40fa8559c' is invalid according to its datatype 'http://WPCommontypes:ST_Guid' - The Pattern constraint failed.

Note that the error message you receive might be a little different depending on what value is not defined properly. You are in luck though, as there is a simple fix for this.

You can fix the issue by modifying the WMAppManifest.xml file in your Windows Phone project. You need to add curly braces (are there any other style?) around the GUID values for the ProductID and PublisherID attributes. In some cases there might be other GUID values that are missing the curly braces too so add them there as well.

And that's it! This should resolve your error and get you on your path to a successfully building application :)

Cheers,
Paras Wadehra
Microsoft MVP - Windows Platform Development
http://twitter.com/ParasWadehra
https://www.facebook.com/WindowsPhoneDeveloper
My WP Apps